Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C default arguments

Is there a way to specify default arguments to a function in C?

like image 397
Nathaniel Flath Avatar asked Sep 24 '09 14:09

Nathaniel Flath


People also ask

What is a default argument in C?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument.

What type are C functions by default?

By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.

What do you mean by default argument?

In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this is the case in the C programming language).

What are the advantages of default arguments?

provides a simple, natural and effective programming style; in some cases, the default arguments are a shortened form of the function overload. This, in turn, improves the readability of the program code and simplifies the function call.


1 Answers

Wow, everybody is such a pessimist around here. The answer is yes.

It ain't trivial: by the end, we'll have the core function, a supporting struct, a wrapper function, and a macro around the wrapper function. In my work I have a set of macros to automate all this; once you understand the flow it'll be easy for you to do the same.

I've written this up elsewhere, so here's a detailed external link to supplement the summary here: http://modelingwithdata.org/arch/00000022.htm

We'd like to turn

double f(int i, double x) 

into a function that takes defaults (i=8, x=3.14). Define a companion struct:

typedef struct {     int i;     double x; } f_args; 

Rename your function f_base, and define a wrapper function that sets defaults and calls the base:

double var_f(f_args in){     int i_out = in.i ? in.i : 8;     double x_out = in.x ? in.x : 3.14;     return f_base(i_out, x_out); } 

Now add a macro, using C's variadic macros. This way users don't have to know they're actually populating a f_args struct and think they're doing the usual:

#define f(...) var_f((f_args){__VA_ARGS__}); 

OK, now all of the following would work:

f(3, 8);      //i=3, x=8 f(.i=1, 2.3); //i=1, x=2.3 f(2);         //i=2, x=3.14 f(.x=9.2);    //i=8, x=9.2 

Check the rules on how compound initializers set defaults for the exact rules.

One thing that won't work: f(0), because we can't distinguish between a missing value and zero. In my experience, this is something to watch out for, but can be taken care of as the need arises---half the time your default really is zero.

I went through the trouble of writing this up because I think named arguments and defaults really do make coding in C easier and even more fun. And C is awesome for being so simple and still having enough there to make all this possible.

like image 91
bk. Avatar answered Sep 18 '22 04:09

bk.