Another academic question - ANSI C standard on optional function parameters
-
Thanks, but I may have messed up my question. What I was looking for - is this syntax ANSI C standard function(int a, int b = 0) Where passing the second parameter in function call is optional. Cheers Vaclav
-
No, that's C++; there is no overloading in C. See http://msdn.microsoft.com/en-us/library/cts394c0.aspx[^].
Now I am totally lost. I thought overloading was having same function name with different types of variables. Such as function(int) function(void*) function(double) That works fine in Arduino C++ compiler.( And right now I have a bug in my code because I am using it). I am assuming Arduino compiler is C++, did not really try to find out. But since it works with classes....it must be C++ based. Cheers Vaclav
-
Now I am totally lost. I thought overloading was having same function name with different types of variables. Such as function(int) function(void*) function(double) That works fine in Arduino C++ compiler.( And right now I have a bug in my code because I am using it). I am assuming Arduino compiler is C++, did not really try to find out. But since it works with classes....it must be C++ based. Cheers Vaclav
Vaclav_Sal wrote:
I thought overloading was having same function name with different types of variables.
Such as
function(int)
function(void*)
function(double)You thought right.
Vaclav_Sal wrote:
That works fine in Arduino C++ compiler
As it should: after all is a
C++
compiler. You have to understand thatC
andC++
are two different programming languages. WhileC++
supports function overloading,C
does not.THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
Now I am totally lost. I thought overloading was having same function name with different types of variables. Such as function(int) function(void*) function(double) That works fine in Arduino C++ compiler.( And right now I have a bug in my code because I am using it). I am assuming Arduino compiler is C++, did not really try to find out. But since it works with classes....it must be C++ based. Cheers Vaclav
-
What is a current C ( ANSI C) standard on putting optional parameters / attributes into function? Or is there such a thing - ANSI standard _ anymore? "Popular" microprocessor compiler does not allow such thing and "workaround" is function overloading. Which IMHO in memory limited device adding more similar functions into code does not make much sense, unless you are memory chips salesman. Cheers Vaclav
Since C doesn't allow overloading to start with, the only way to call a function with alternate sets of parameters is by thinking up a pattern on how you pass these parameters in a less rigid way. One way has already been mentioned: passing an array of void pointers. The disadvantage is that you have to figure out what is what, including type, and if more than one parameter is optional you need to additionally pass some information about which of the parameters were skipped. Pretty much the same goes for vararg lists. A slightly better approach that offers strict control over types and meaning of parameters is passing a struct that contains all possible parameters: write one function that allocates and/or initializes the struct with defaults, and in the code overwrite all values that you want different, then pass the struct to your processing function. If you have mandatory parameters without sensible defaults (e. g. array sizes), make sure to pass these to the intialization function.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
The following defintion is implied overloading since you can call it in one of two ways.
function(int a, int b = 0);
//
function(1, 2); // two parameters with explicit values//
function(1); // one explicit and one implicit (0) parameterOK, I did try this void TestFunctionA (int a, int b = 100) { a += 1; b += 10; Serial.print(b); } void TestFunctionB (int a) { a += 1; // b += 10; Serial.print(a); } and calls TestFunctionB (10); // int a, int b = 100) TestFunctionA(10,234); And the compiler said too many arguments in function call But if I change the b to float it compiles and it does makes sense to me. Partially. If the b parameter b is not passed, than the function A will be executed and compiled. Confusing, yes. But that was not the reason I asked. In "real" C compiler when a parameter is declared optional the code will compile without having to change all function calls to include the optional parameter. But I still do not know if that is ANY standard or just MS compiler specific. But Arduino compiler expects the "optional" parameter, so it is really not optional. Cheers Vaclav
-
OK, I did try this void TestFunctionA (int a, int b = 100) { a += 1; b += 10; Serial.print(b); } void TestFunctionB (int a) { a += 1; // b += 10; Serial.print(a); } and calls TestFunctionB (10); // int a, int b = 100) TestFunctionA(10,234); And the compiler said too many arguments in function call But if I change the b to float it compiles and it does makes sense to me. Partially. If the b parameter b is not passed, than the function A will be executed and compiled. Confusing, yes. But that was not the reason I asked. In "real" C compiler when a parameter is declared optional the code will compile without having to change all function calls to include the optional parameter. But I still do not know if that is ANY standard or just MS compiler specific. But Arduino compiler expects the "optional" parameter, so it is really not optional. Cheers Vaclav
-
OK, I did try this void TestFunctionA (int a, int b = 100) { a += 1; b += 10; Serial.print(b); } void TestFunctionB (int a) { a += 1; // b += 10; Serial.print(a); } and calls TestFunctionB (10); // int a, int b = 100) TestFunctionA(10,234); And the compiler said too many arguments in function call But if I change the b to float it compiles and it does makes sense to me. Partially. If the b parameter b is not passed, than the function A will be executed and compiled. Confusing, yes. But that was not the reason I asked. In "real" C compiler when a parameter is declared optional the code will compile without having to change all function calls to include the optional parameter. But I still do not know if that is ANY standard or just MS compiler specific. But Arduino compiler expects the "optional" parameter, so it is really not optional. Cheers Vaclav
This doesn't compile as C code in VisualStudio 2010. If it did for you, you compiled as C++. Hint: in VS 2010 you can't create a C project directly, only a C/C++ project that will contain *.cpp files which by default will be compiled as C++. You can override that behaviour by explicitely forcing C compilation in the Compiler->Advanced settings tab of the project. Or you can put your C code in a *.C file (that you will have to explicitely create, of course).
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)