Home > Software design >  Calling function with macro in C
Calling function with macro in C

Time:11-29

I have function in C with 3 params. I want to call that function with pre-defined values. Is there any solution?

My idea which does not work.

#define PRE_DEFINED_MACRO   (10, 15 , true)

void myFunc(uint8_t temp, uint32_t value, bool valid)
{
  .... 
}
....
//call the function like that
myFunc(PRE_DEFINED_MACRO);

CodePudding user response:

Your macro already includes parentheses, so when this ...

myFunc(PRE_DEFINED_MACRO);

... is expanded, the result is:

myFunc((10, 15 , true));

You could do this instead:

myFunc PRE_DEFINED_MACRO;

I find that pretty confusing, however. Personally, I would prefer to remove the parentheses from the macro's replacement text:

#define MYFUNC_ARGS  10, 15 , true

// ...

myFunc(MYFUNC_ARGS);

That makes it much clearer that the call to myFunc() is, in fact, a function call. The choice of macro name also makes the intended purpose of the macro much clearer.

CodePudding user response:

To just replace 3 arguments with a macro:

#define PRE_DEFINED_MACRO   10, 15, true

The reason why you can't use a parenthesis is because it would expand as myFunc((10, 15, true)); and the compiler reads that as "the first parameter is obtained from a chained series of comma operators and the rest of the parameters are missing". What does the comma operator , do?

As for always giving a certain function the same default arguments and optionally omit some arguments (like in C ), it's a bit more intricate. See C default arguments, including this late answer that I posted there recently myself.

CodePudding user response:

Another, not uncommon solution would be to use a function-like macro right away, e.g.

#define MY_FUNC_DEFAULTS() myFunc(10, 15, true)

This looks also fairly clean, retains the function syntax etc. Note that the macro expansion does not end with a semicolon so that it can (and must) be provided at the call site in the natural fashion: if(cond) MY_FUNC_DEFAULTS(); else g(); works as expected.

Because you originally asked about C : I find some of the OCD types here a bit annoying. All preprocessor solutions provided in the answers here work equally well in both languages. Nonetheless, the preprocessor is text replacement and as such inherently unsafe. Therefore, C provides means to avoid using it. In this case one could provide default arguments in the function declaration:

void myFunc(uint8_t temp = 10, uint32_t value = 15, bool valid = true);

This essentially overloads the function: It defines a set of four distinct functions that take 3, 2, 1 or 0 arguments, respectively. All of the calls

myFunc();
myFunc(1);
myFunc(1,2);
myFunc(1,2,false);

are now possible.

Such declarations could be present in different translation units with different default values. The default values are defined at the caller side and are not part of the function signature. (Whether that would be recommended is debatable though.)

  • Related