I understand that there are no guarantees regarding the order which the parameters of a function will be called, but, isn't it guaranteed that if there's a function call as parameter, that function will be called first?
I help students on the laboratory of an introductory subject on programming, and they were supposed to create a recursive factorial function which receives the n (for n!) and a pointer to an integer that will be used to count function calls and then they are supposed to print the results (n, n! and count).
Many were complaining that their use of pointers were wrong so I looked at the code and they were all like this:
int fat(int n,int *count)
{
(*count)++;
if(n>1)
{
return n * fat(n-1,count);
}
return 1;
}
int main()
{
int n, count=0;
do
{
printf("Write n for fat (n >= 0): ");
scanf("%d", &n);
}while(n<0);
printf("Input: %d. Output: %d.\nFunction called %d times.\n\n", n, fat(n, &count), count);
printf("%d times\n",count);
return 0;
}
Compiling with gcc (Debian 4.7.2-5) 4.7.2 the output is:
Write n for fat (n >= 0): 4
Input: 4. Output: 24.
Function called 0 times.
4 times
So, if fat was supposed to run first, the "Function called..." should print "Function called 4 times" instead of 0.
So, my question is:
Even though it's guaranteed that a function call in a parameter will run before the "receiving" one, it is still uncertain whether it runs before looking at parameters that aren't function calls?
Another strange thing is that this same code prints "Function called 4 times" on xcode...