In which order function parameters are evaluated in C/C++?

Let’s first discuss what we mean when we talk about functions and function parameters. Functions are referred to as a small block of code which can be called from the main block repeatedly to avoid the redundancy of code. First let’s understand functions, their types, and what are function parameters or input arguments.

Functions Introduction

In C programming, functions can be divided into four categories depending upon their return type and parameters. These four categories are listed below,

  • Function with no parameter and don’t returns anything.
    • void function()
  • Function with parameters and don’t returns anything.
    • void function(int a)
  • Function with no parameters but returns a value.
    •  int function()
  • Function with parameters and returns a value.
    • int function(int a)

Parameters

The parameters of a function are defined as the arguments which we pass into the function so that they can be used in the evaluation of the specific task designated to that function. Passing as argument to a function is further divided into 2 types but there details are not the scope of this article, however the names of those types are listed below,

  • Pass argument by value
  • Pass argument by reference

Return value

The return type of the function is the value that the function returns after performing the desired operation on the input parameters. As an example consider the function shown in the figure below. It takes two integers as parameters i.e. as input and returns a single integer which is the sum of the input variables

int sum(int a, int b)
{
	int c; 
	c = a+b;
	return c;
}

Calling this function from the main block will return the sum of the parameters passed in this function, as shown in the figure below.

#include<stdio.h>
int sum(int a, int b)
{
	int c; 
	c = a+b;
	return c;
}
int main(void)
{
	int a;
	a = sum(1,2);
	printf("The sum of the numbers is %d", a);
}

The output of this function is shown in the figure below,

order of functions parameter evaluation

Order of parameter evaluation

The order of evaluation of parameters in C is not defined. We cannot pick a side when talking about the order in which the parameters are evaluated i.e. whether it is from left to right or from right to left. Also there isn’t any legal document available which can prove in which order the parameters of a function are going to be evaluated. Most of the experts say that the order of evaluation of parameters is compiler dependent and there’s always a solution to compiler dependent problems.

Testing Functions Parameters Evaluation

We can run a few tests on the compiler on hand and check that the specific compiler supports what order. Let’s begin with a simple C program. We will pass the same variable in each parameter slot and increment it after its use. Printing such a variable will help us chose which parameter was evaluated first. For instance, refer to the C program shown in the figure below,

#include<stdio.h>
void function(int first, int second, int third) {
   printf("The value of first parameter is: %d\n", first);
   printf("The value of second parameter is: %d\n", second);
   printf("The value of third parameter is: %d\n", third);
}
main() 
{
   int test = 10;
   function(test++, test++, test++);
}

The purpose of this function call on line number 10 is to check which parameter will be evaluated first. But the question is how we will distinguish between the orders of evaluation. It’s quiet simple lets first see the output of this function then I will explain what actually happened here.

order of functions parameter evaluation in C programming

Here the lowest value is against the third parameter which is 10, it means the third parameter is evaluated first and after evaluation, the value of a gets incremented and the control moved toward the second parameter. After evaluating the second parameter the value again gets incremented and the control moves towards first. Hence for my compiler, the order of evaluation of parameters was from left to right that is from last argument towards the first argument. But we cannot say that it will be the same on your compiler too. Therefore, as an assignment think of a different program and check the order of parameter evaluation on your computer.  

More C Programming Tutorials:

  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type

Leave a Reply