Order of evaluation in C++ function parameters


We pass different arguments into some functions. Now one questions may come in our mind, that what the order of evaluation of the function parameters. Is it left to right or right to left?

To check the evaluation order we will use a simple program. Here some parameters are passing. From the output, we can find how they are evaluated.

Example

 Live Demo

#include<iostream>
using namespace std;
void test_function(int x, int y, int z) {
   cout << "The value of x: " << x << endl;
   cout << "The value of y: " << y << endl;
   cout << "The value of z: " << z << endl;
}
main() {
   int a = 10;
   test_function(a++, a++, a++);
}

Output

The value of x: 12
The value of y: 11
The value of z: 10

From this output, we can easily understand the evaluation sequence. At first the z is taken, so it is holding 10, then y is taken, so it is 11, and finally, x is taken. So the value is 12.

Updated on: 30-Jul-2019

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements