Named arguments in C
What if I told you that we can write functions in plain C that support named arguments, like for example in Scala or Python? Take a look at the following code:
The trick here is to create a structure that can hold the arguments of the call,
and implement the function taking this structure instead of the single
arguments. Then, using a combination of a #define
with variable arguments that
create a compound literal, we create the façade that is called as if it were the
actual function:
The struct will live on the caller’s stack in this implementation, but you could also just pass the whole struct into the function, as long as the struct doesn’t contain any arrays that you don’t want to copy every time the function is called.
You can find the whole file in this gist.
Please go here to comment this article.