Question: In C and C++, is there a fixed order for evaluation of parameter to the function? I mean, what do the standards say? Is it `left-to-right` or `right-to-left`?
I am getting confusing information from the books.

Is it necessary that `function call` should be implemented using `stack only`? What do the C and C++ standards say about this?

Answer: 
> 
> In C/C++ is there a fixed order for evaluation of parameter to the function. I mean what does standards says is it left-to-right or right-to-left . I am getting confusing information from the books.
> 
> 
> 

No, the order of evaluation of function parameters (and of two sub-expressions in any expression) is *unspecified behaviour* in C and C++. In plain English that means that the left-most parameter could be evaluated first, or it could be the right-most one, and *you cannot know which order that applies for a particular compiler*. 

Example:

```
static int x = 0;

int* func (int val)
{
  x = val;
  return &x;
}

void print (int val1, int val2)
{
  cout << val1 << " " << val2 << endl;
}

print(*func(1), *func(2));

```

This code is very bad. It relies of order of evaluation of print's parameters. It will print either "1 1" (right-to-left) or "2 2" (left-to-right) and *we cannot know which*. The only thing guaranteed by the standard is that both calls to func() are completed before the call to print().

The solution to this is to be aware that the order is unspecified, and write programs that don't rely on the order of evaluation. For example:

```
int val1 = *func(1);
int val2 = *func(2);
print(val1, val2); // Will always print "1 2" on any compiler.

```

---

> 
> Is it necessary that function call should be implemented using stack only. what does C/C++ standards says about this.
> 
> 
> 

This is known as "calling convention" and nothing that the standard specifies at all. How parameters (and return values) are passed, is entirely up to the implementation. They could be passed in CPU registers or on the stack, or in some other way. The caller could be the one responsible for pushing/popping parameters on the stack, or the function could be responsible.

The order of evaluation of function parameters is only somewhat associated with the calling convention, since the evaluation occurs *before* the function is called. But on the other hand, certain compilers can choose to put the right-most parameter in a CPU register and the rest of them on the stack, as one example.
Just only to speak for **C** language, the order of evaluation inside the function parameters depend on compiler. from **The C Programming Language** of **Brian Kernighan** and **Dennis Ritchie**;

> 
> Similarly, the order in which function arguments are evaluated is not
>  specified, so the statement
> 
> 
> `printf("%d %d\n", ++n, power(2, n)); /*WRONG */`
> 
> 
> can produce different results with different compilers,
>  depending on whether n is incremented before power is called. The
>  solution, of course, is to write
> 
> 
> `++n;`
> 
> 
> `printf("%d %d\n", n, power(2, n));`
> 
> 
>