NXC: functions with different numbers of parameters?
Posted: 02 Jul 2012, 21:08
by HaWe
hi,
how is it possible to define a macro or to declare a function with optionally different numbers of parameters passed to it?
e.g., like to mimic TextOut where it's possible to have optionally 3 or 4 parameters:
TextOut(0,0, "hello");
vs.
TextOut(0,0, "hello", DRAW_OPT_INVERS);
I want to use this feature e.g., to pass diffenent numbers of variables to a max function (or macro)
float max(float f1, float f2)
vs.
float max(float f1, float f2, float f3)
or to pass different numbers of values to print to screen
#define printf_(fmtstr, val1)
vs.
#define printf_(fmtstr, val1, val2)
Re: NXC: functions with different numbers of parameters?
Posted: 02 Jul 2012, 22:30
by mcsummation
This is from the help for "function":
NXC supports specifying a default value for function arguments that are not struct or array types. Simply add an equal sign followed by the default value. Specifying a default value makes the argument optional when you call the function. All optional arguments must be at the end of the argument list.
In your case of "max", you could make the default values very large negative numbers.
Re: NXC: functions with different numbers of parameters?
Posted: 03 Jul 2012, 06:45
by HaWe
I know that this is the default setting,
but as I wrote: e.g., TextOut already allows different numbers of parameters, and what I want is : to mimic this feature.
So how is this done by TextOut (or NumOut, or maybe others) and how can use this trick for my own functions?
Re: NXC: functions with different numbers of parameters?
Re: NXC: functions with different numbers of parameters?
Posted: 03 Jul 2012, 20:31
by mcsummation
I think FLT_MIN should be -1E+37.
1E-37 is the smallest float, not the minimum. You want the 2 numbers to be the farthest from zero.
Re: NXC: functions with different numbers of parameters?
Posted: 03 Jul 2012, 21:19
by HaWe
yes, sure, you're right, thx!
(indeed C libraries use FLT_MIN as the smallest number close to zero, not the absolute max negative value what I actually wanted)
But if correct it, I get a new error: Invalid parameter syntax with default values
so -1E37 actually should work
(in NXC the float max/min constants are not defined yet)
Re: NXC: functions with different numbers of parameters?
Posted: 05 Jul 2012, 13:16
by HaWe
compiler bug or programmer's bug?^^
Re: NXC: functions with different numbers of parameters?
Posted: 05 Jul 2012, 21:24
by spillerrec
I just installed Windows on this laptop so can't check, but it seems like it is the compiler which easily messes up with scientific notification. I haven't had issues with default values before either.
With macro functions you can use "..." as the last parameter to accept variable input. Notice that this only works if "..." is used in opcodes which accepts a variable amount of inputs. (So while it is possible to use "..." in functions in C, this wouldn't be possible on the NXT firmware.) I cannot remember how to use it and I'm not sure if it is documented (so you might have to look in the header files). But you could make your macro like this:
I don't know how to do "return" in macro functions but I think it is possible. I might you have to pass it to some internal variable or something... But if you can get it working it will be much more efficient than your current code.