10-25-2010 11:29 AM
Hi all,
I would like to make some function that is passed a void for most cases, but can also be passed an integer. Sorry for this basic question, but is this possible?
my prototype would be something like..
void getParameters(void);
and would in most cases be called by "getParameters();" but I can also do "getParameters(1);" if I so wish.
I know I could simply make a prototype like..
void getParameters(int myInt);
and always pass it as "getParameters(0);" but I was just curious about this case.
Thanks in advance,
Turbo
10-25-2010 11:55 PM - edited 10-25-2010 11:57 PM
Hi Turbo,
The simple answer is, No.
You cannot use both calls in the same code without getting a compiler error.
It is possible in C++, thanks to the concpet called function overloading, in which compiler decides which version of the function to call depending on the parameter types.
However, C (being the language of CVI) does not allow 2 separate declarations of the same function.
One thing you may consider is maybe is using the void pointer. e.g: getParamaters (void *p);
You can pass NULL or a valid pointer to any type while calling the function.
Then, within the function you can cast the pointer to the type you want and use the value it refers to.
As in all pointer operations, be careful 😉