C

In C Programming, A Function Is Called By Its Value

In C programming, calling a function by value is the default method. Before we get into function call by value, it’s important to understand the terminology we’ll be using:
The parameters that appear in function calls are known as actual parameters.
Formal parameters are the parameters found in function declarations.
As an example:

#include <stdio.h>
int sum(int a, int b)
{
     int c=a+b;
     return c;
}

int main(
{
    int var1 =10;
    int var2 = 20;
    int var3 = sum(var1, var2);
    printf("%d", var3);

    return 0;
}

The formal parameters in the preceding example are variables a and b. (or formal arguments). The actual arguments are variables var1 and var2 (or actual parameters). The values can also be the actual parameters. In this case, 10 and 20 are actual parameters, similar to sum(10, 20).
This guide will go over function call by value. If you want to learn more about the call by reference method, check out this guide: function call by reference.
Let’s get back to business.
What exactly is Function Call By Value?
When we pass the actual parameters to a function when calling it, this is referred to as function call by value. The values of the actual parameters are copied to the formal parameters in this case. As a result, operations performed on the formal parameters have no effect on the actual parameters.

Function call by Value Example

As previously stated, in a call by value, the actual arguments are copied to the formal arguments, so any function operation on arguments has no effect on the actual parameters. To illustrate, consider the following:

#include <stdio.h>
int increment(int var)
{
    var = var+1;
    return var;
}

int main()
{
   int num1=20;
   int num2 = increment(num1);
   printf("num1 value is: %d", num1);
   printf("\nnum2 value is: %d", num2);

   return 0;
}

Output:

num1 value is: 20
num2 value is: 21

Explanation
We passed the variable num1 when we called the method, but because we used the call by value method, only the value of num1 is copied to the formal parameter var. As a result, changes to the var are not reflected in num1.

Example 2: Using Function Call by Value to swap numbers

#include <stdio.h>
void swapnum( int var1, int var2 )
{
   int tempnum ;
   /*Copying var1 value into temporary variable */
   tempnum = var1 ;

   /* Copying var2 value into var1*/
   var1 = var2 ;

   /*Copying temporary variable value into var2 */
   var2 = tempnum ;

}
int main( )
{
    int num1 = 35, num2 = 45 ;
    printf("Before swapping: %d, %d", num1, num2);

    /*calling swap function*/
    swapnum(num1, num2);
    printf("\nAfter swapping: %d, %d", num1, num2);
}

Output:

Before swapping: 35, 45
After swapping: 35, 45

Why do variables retain their original values even after the swap?
The reason is the same – for num1 and num2, the function is called by value. As a result, var1 and var2 are swapped (not num1 & num2). Actual parameters are simply copied into the formal parameters, as in call by value.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button