I heard from most teachers that in a same expression containing multiple functions calls there is no definite way to tell which function is called first.
So,i made this program to check whether the functions are called in a definite pattern or not,
#include<stdio.h>
int a=0;
int func1()
{
a=a+1;
return a;
}
int func2()
{
a=a+2;
return a;
}
int func3()
{
a=a+3;
return a;
}
int main()
{
int i;
i=(func1()+func2()+func3());
printf("\n%d",i);
return 0;
}
As you can see if we assume the functions calls to be made from left to right in that expression, then only the result would be 10. ( which in real is also 10 ).
To further confirm things up, i made a slight change in line number 23 and changed the call pattern as you can see here,
#include<stdio.h>
int a=0;
int func1()
{
a=a+1;
return a;
}
int func2()
{
a=a+2;
return a;
}
int func3()
{
a=a+3;
return a;
}
int main()
{
int i;
i=(func3()+func2()+func1());
printf("\n%d",i);
return 0;
}
Here also as expected the result came out as 14.
So, does that mean that the function calls follow a pattern (they are called from left to right in a expression)?
If you only test this in one compiler, you will never see a difference and you will always find a pattern.
But even if you test it in multiple compilers, you can never be sure. This is because what you are being told is what the standard for C++ covers. The standard doesn't specifically say in what order a compiler needs to evaluate the functions. This means that any developer creating a C++ compiler is free to take one of several routes and still comply with the C++ standard.
So in conclusion: It is not what you see out there, it is what is guaranteed in the C++ standard.
Disclaimer: I am no expert at this, so I am talking about this from what I have learned here from others. If I'm not correct, by all means, correct me.
I'd like to point out that this is rarely an issue. In fact I cannot think of a single time this has affected an application I've written. As a rule you try to avoid using global variables because of this, especially if multiple functions address them, but that becomes a habbit so fast that you don't even think about it. Although it's good that your teachers are mentioning it, this fact gets buried a lot.
@soheilghafurian: When a variable is defined outside of all functions, it becomes global. This means every function has access to it. If the variable were defined in the main() function, then the other functions would not have access to it.
Note that cases using the built in operator&& and operator|| are treated specially. They are guaranteed to evaluate from left to right. The same holds for the sequence operator (,) and ?:
I have never tried, but if I remember right, you should never overload operator&& or operator|| as the overloads are not specially treated, so standard coding practices which rely on short circuit evaluation (to stop the deref-ing of null pointers, etc.) could end up going horribly wrong!
Andy
P.S. I have a feeling that the above example is always going to be safe, at least in optimized build, as the compiler will inline all the above code pretty much as-is. You probably need a harder algorithm for the compiler to go to any trouble!