Sat Jan 11 2020

Call by Value vs Call by Reference

Call by Value vs Call by Reference

In C language, there are two ways to passing arguments in a function, that are Call by Value and Call by Reference. Before we start to know these two process, we have to clear the concept of argument and function.

So, what is function and argument?

Function: Function is a good programming style in which we can write reusable code that can be called whenever require.

Argument: Functions are generally written for a particular operation with one or more inputs. Like we write a logical function to check if the number is prime or not which is not dependent on any specific number but can be used for any positive integers. We reuse this logic by providing an input number. This input is called function argument.

Let’s back to the main topic, we already discussed that C provides two ways of passing arguments to a function. These are -

Call by value

In this method a copy of each of the actual arguments is made first then these values are assigned to the corresponding formal arguments. That means the changes made by the called function have no effect on the values of actual arguments in the calling function. When you passed a value to the function it is locally stored by the function parameter in stack memory location. If you change the value of function parameter, then it changed for the current function only.

How the call by value works?

  • While passing parameters using call by value, the copy of original parameter is created and passed to the called function.

  • Any update made inside the method will not affect the original value of the variable in the calling function.

  • Copy of the original values are passed to the function and the values are copied into the variable of called function respectively.

  • As their scope is limited to the only function, so they cannot alter the values inside the main function.

Example -

#include

int factorial(int);
int main() {
    long int no, f = 1;
    printf("Calculate Factorial\n");
    printf("Enter a number:\n");
    scanf("%ld", &no);

    /* If the given number is not zero then factorial calculation start */
    if(no != 0) {

        /* Call factorial function by passing integer value and receive result in 'f' variable */
        f = factorial(no);
        printf("Factorial: %ld\n", f);
    }

    /* If the given number is zero then factorial will be one */
    else
        printf("Factorial: 1\n");
    return 0;
}

/* Recursive function */
int factorial(int no) {
    if(no == 1)
        return 1;
    else
        /* Call the factorial function inside in it */
        return(no * factorial(no - 1));
}

 

/****** Output ******/
Calculate Factorial

Enter a number:

5

Factorial: 120

Call by reference

In this method, when we call a function by passing the addresses of actual parameters then this way of calling the function is known as call by reference. In call by reference, the operation performed on formal parameters affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters.

How the call by reference works?

  • While passing parameter using call by address scheme, you are passing the actual address of the variable to the called function.

  • Any updates made inside the called function will modify the original copy since you are directly modifying the content of the exact memory location.

Example -

#include

/* Function 'swap' with pointer parameter */
void swap(int *a, int *b) {
    int temp;

    /* Swapping values using 'temp' variable */
    temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x, y;
    printf("Enter a value for X:\n");
    scanf("%d", &x);
    printf("Enter a value for Y:\n");
    scanf("%d", &y);
    printf("Before swapping\n");
    printf("X = %d\nY = %d\n",x, y);

    /* Passing reference(memory address) of the variables */
    swap(&x, &y);
    printf("After swapping\n");
    printf("X = %d\nY = %d\n",x, y);
    return 0;
}

 

/****** Output ******/
Enter a value for X: 5

Enter a value for Y: 10

Before swapping

X = 5

Y = 10

After swapping

X = 10

Y = 5

Let’s find out the differences between call by value and call by reference in c language -

  • A duplicate copy of the value is passed to the function in a call by value, where the actual copy or an address of value is passed to the function is known as call by reference.

  • In call by value, changes made inside the function is not reflected on other functions. But in the call by reference, changes made inside the function and it is reflected outside the function.

  • Actual and formal arguments will be created in different memory location when processing call by value. But in the call by reference, it will be using same memory location.

Usage

The call by reference use pointer, so there is no doubling of the memory used by the variables. Lowering the memory footprint is always a good thing. So it makes sense that why not make all the parameters call by reference?

Two reasons why this is not good for and need to choose between call by value and call by reference. The reasons are side effects which are usually caused by inadvertently changes that are made to a call by reference parameter.  And most cases you want the data to be private and that someone calling a function only be able to change if you want it. So it is better to use a call by value by default. But use call by reference if the data changes are expected.

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.