Function arguments in C – Call by value and Call by reference

Function arguments are the inputs passed to a function. A function must declare variables to accept passed arguments. A variable that accepts function argument is known as function parameter.

In programming function argument is commonly referred as actual parameter and function parameter is referred as formal parameter. I will be using these words interchangeably throughout this series of C programming tutorial.

Actual and formal parameter

In C programming you can pass value to a function in two ways.

  1. Call by value
  2. Call by reference

Call by value

Call by value is the default mechanism to pass arguments to a function. In Call by value, during function call actual parameter value is copied and passed to formal parameter. Changes made to the formal parameters does not affect the actual parameter.

To understand this let us consider an example to swap two numbers using function.

Example program using call by value

/**
 * C program to swap two numbers using call by value
 */

#include <stdio.h>

/* Swap function definition */
void swap(int num1, int num2)
{
    int temp;

    printf("In Function values before swapping: %d %d\n", num1, num2);

    temp = num1;
    num1 = num2;
    num2 = temp;

    printf("In Function values after swapping: %d %d\n\n", num1, num2);
}

/* main() function definition */
int main()
{
    int n1, n2;

    /* Input two integers from user */
    printf("Enter two numbers: ");
    scanf("%d%d", &n1, &n2);

    /* Print value of n1 and n2 in before swapping */
    printf("In Main values before swapping: %d %d\n\n", n1, n2);

    /* Function call to swap n1 and n2 */
    swap(n1, n2);
    printf("In Main values after swapping: %d %d", n1, n2);

    return 0;
}

Output -

Enter two numbers: 10 20
In Main values before swapping: 10 20
In Function values before swapping: 10 20
In Function values after swapping: 20 10
In Main values after swapping: 10 20

In the above program swap() function does not alter actual parameter value. Before passing the value of n1 and n2 to the swap() function, the C runtime copies the value of actual parameter n1 and n2 to a temporary variable and passes copy of actual parameter. Therefore inside the swap() function values has been swapped, however original value of n1 and n2 in main() function remains unchanged.

Call by reference

In Call by reference we pass memory location (reference) of actual parameter to formal parameter. It uses pointers to pass reference of an actual parameter to formal parameter. Changes made to the formal parameter immediately reflects to actual parameter.

Let us again consider the above swap() function to swap values of two variables using call by reference.

Note: To understand below program you must have basic knowledge of pointers.

Example program using call by reference

/**
 * C program to swap two numbers using call by reference
 */

#include <stdio.h>

/**
 * *num1 - pointer variable to accept memory address
 * *num2 - pointer variable to accept memory address
 */
void swap(int * num1, int * num2)
{
    int temp;

    printf("In Function values before swapping: %d %d\n", *num1, *num2);

    temp  = *num1;
    *num1 = *num2;
    *num2 = temp;

    printf("In Function values after swapping: %d %d\n\n", *num1, *num2);
}

/* main() function declaration */
int main()
{
    int n1, n2;

    printf("Enter two numbers: ");
    scanf("%d%d", &n1, &n2);

    printf("In Main values before swapping: %d %d\n\n", n1, n2);

    /*
     * &n1 - & evaluate memory address of n1
     * &n2 - & evaluate memory address of n2
     */
    swap(&n1, &n2);

    printf("In Main values after swapping: %d %d", n1, n2);

    return 0;
}

Output -

Enter two numbers: 10 20
In Main values before swapping: 10 20
In Function values before swapping: 10 20
In Function values after swapping: 20 10
In Main values after swapping: 20 10

In above example instead of passing a copy of n1 and n2, I am passing reference to swap() function. Operations performed on formal parameter is reflected to actual parameter (original value). Hence, actual swapping is performed inside swap() as well as main() function.

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>