vivek.m
It wont work in C++ as in C++, it's necessary to declare function before using. Moreover, function arguments while calling that function must match with the function declaration.
Yes this is correct, the code does not work in C++. C++ has strong type chacking and it's necessary that the function is defined (or atleast declared) before it's use.
vivek.m
On old C compilers, your program may work as C allows dummy arguments. I think this behavior is borrowed from FORTRAN and gives the developer flexibility to add new arguments to functions at later date without actually modify the existing code. The C code wont shout about the arguments until you provide the function with that name.
Yes, this is also correct. Just to add more details:
1. The code is 'C' code and hence it is not necessary to give function prototype. This justifies the working of code in absence of prototype.
2. According to K&R,
"If there is no function prototype, a function is implicitly declared by its first appearance in an expression. If a name that has not been previously declared occurs in an expression and is followed by a left parentheses, it is declared by context to be a function name, the function is assumed to return an int, and nothing is assumed about its arguments, all parameter checking is turned off. This special meaning of the empty argument list is intended to permit older C programs to compile with new compilers. In the older programs the parameters are named between the parentheses, and their types are declared before opening the left brace; undeclared parameters are taken as int."
This justifies the working of code even after parameter mismatch.
3. When we do not provide sufficient parameters it takes the values from the stack (the value may be garbage for that particular function). This is similar to something that happens with printf() when the number of arguments and format specifiers varies, e.g., printf("%d %d", a); works fine and it takes the second value from the function stack.
vivek.m
If the caller passes the argument, it is used. If the caller does not pass the argument, it holds either garbage or null value (not sure about this).
As i mentioned in point 3 above, it will hold the garbage value.
vivek.m
I see this flexibility as very limiting as you can provide dummy arguments to very limited types - int/float/char only. If you put any pointer as dummy argument and then using it inisde code will lead to crash.
Not necessary, as in the example i have given, i have passed a char to char pointer and it works. Similarly, i can pass a int pointer to int and it will work and use it suitably. The variable in the function definition should be big enough to hold the value passed during the function call and it will work.
-Pradeep