Call C function by value method

We can call function by value in C language. This is the most normal and default way to call a function. When we pass some actual parameters in a function then it is called function call by value method. Before getting started we should know what is actual parameters and formal parameters. So, lets try to understand through a program.

// call function by value in C programming
#include <stdio.h>
int get_sum(int x, int y){    // formal parameters
     int total = x + y;
     return total;
}

int main(){
    int a = 15;
    int b = 35;
    int sum = get_sum(a, b);    // actual parameters
    printf("Sum is = %d\n", sum);
    return 0;
}

See the output bellow

Call function by value in C, function calling by value in c

In this above example x and y are called formal parameters where a and b are called actual parameters. We can use direct number like 15 and 35 instead of a and b. Then this 15 and 35 will be the actual parameter of this function.

Here in this article we will learn more about call function by value in C. So, lets get started with this method.

How function call by value method works?

The value of actual parameters are copied to formal parameters when we call a function by value. For this the function does not affect the actual parameters at all. Consider the following program.

// How function call by value method works?
#include<stdio.h>
void value_adding(int x){
    printf("\nValue of a inside function before adding a = %d\n", x);
    x = x + 10;
    printf("\nValue of a inside function after adding a = %d\n", x);
}

int main(){
    int a = 20;
    printf("\nBefore calling function a = %d \n", a);
    value_adding(a);
    printf("\nAfter calling function value of a = %d \n", a);
return 0;
}

Output of this call by value function

call function by value, c function call by value method

Explanation of program : Here we have assigned value of a is = 20. Then we have printed the value which is a = 20. Then we have gone inside the function by calling it with value a.

Inside the function we have printed again the value of a before adding 10 with it. Then we added 10 with a which means now a = 30. After executing all the code of function the value of a remain unchanged as before which we have assigned 20.

So, we can easily understand that the value of actual parameter does not change when we call a function by value. Rather the value of actual parameters are copied to formal parameters for executing the function without changing the value of actual parameters.

Now we will see an example program to learn more about call function by value method. Then we will go to the other method of calling a function which is call function by reference.

So, lets see an example of C program to understand function call by value.

Swap numbers using call function by value method

// Swapping of two numbers using call function by value method
#include <stdio.h>
void swap_numbers(int i, int j){
  int temp;
  temp = i;
  i = j;
  j = temp;
  printf("\nAfter swapping values in function\nfirst = %d\nsecond = %d\n", i, j);
}

int main(){
  int first = 30;
  int second = 50;
  printf("Before swapping the values in main function\nfirst = %d\nsecond = %d\n", first, second);

  swap_numbers(first, second);

  printf("\nAfter swapping values in main function\nfirst = %d\nsecond = %d\n", first, second);
  return 0;
}

If you compile and run this program, then you will see the following output.

Before swapping the values in main function
first = 30
second = 50

After swapping values in function
first = 50
second = 30

After swapping values in main function
first = 30
second = 50