Parameter Passing:Call by Value and Call by Reference

When a function is called, the execution control is transferred from the calling function to the called function, and once it is done the control returns back to the caller function.

During the flow of control of execution from caller function to called function it may carry one or more data known as parameters.

Parameters are data values that are passed from the calling function to called functions.

Please refer to the function and function types for a better understanding of the sections below.

In C, there are two types of parameters:

  • Actual Parameters: Parameters specified in the caller function
  • Formal Parameters: Parameters specified in the called function

In the C programming language, there are two methods to pass parameters from calling function to called function, those are:

  • Call by Value
  • Call by reference

C has one way of passing value to a function that is call by value but call by value can be manipulated in such a way that it looks like call by reference but the reality it is not.

Call by Value:

In this method, a new copy of the variable is created at the called function that is at formal parameters. Since in this case a new copy of the variable is being created, thus any changes made inside the called function is not reflected outside the functions that is at the caller function.

It copies the actual value of the argument into the formal parameters of the function.

Sample Program:

#include <stdio.h>

void call_by_value (int number)
{
   printf("\n Value before adding is: %d", number);
   number = number + 5;
   printf("\n Value after addition is: %d", number);
}

int main()
{
  int num = 10;
  printf("\n Value before calling the function is: %d", num);
  call_by_value (num);
  printf("\n Value after calling the function is: %d\n", num);
  return 0;
}

Output:

Value before calling the function is: 10
Value before adding is: 10
Value after addition is: 15
Value after calling the function is: 10

The above program variable “num” is the actual parameter and variable “number1” is the formal parameter. The value of the actual parameter is copied into the formal parameter as if the formal parameter is altogether a new variable. Hence, the values are not reflected outside the called function.

Call by reference:

In this method, the address of the actual argument is copied to the formal argument of the function. The address is used to access the memory locations of the actual parameters in the called function.

Since the address of the actual argument is used in the called function, thus any changes made inside the called function are reflected even in the caller function.

In this the formal parameter must be pointer variable.

Sample Code:

#include <stdio.h>

void call_by_reference (int *number)
{
   printf("\n Value before adding is: %d", *number);
   *number = *number + 5;
   printf("\n Value after addition is: %d", *number);
}

int main()
{
  int num = 10;
  printf("\n Value before calling the function is: %d", num);
  call_by_reference (&num);
  printf("\n Value after calling the function is: %d\n", num);
  return 0;
}

Output:

Value before calling the function is: 10
Value before adding is: 10
Value after addition is: 15
Value after calling the function is: 15

In the above example, we can see that the value of the variable “num” is modified in the called function and the same value is reflected outside in the called function as both the actual and formal parameters refers to the same address.

Points to be remembered:

  • C does not have call-by-reference.
  • A call-by-reference is one of the applications of pointers.
  • Call-by-reference saves memory.
  • In interview specifically mention that there is only one way of passing parameters(call-by-value) but it can be manipulated to behave as pass-by-reference using pointer

Example:

Passing a structure will put the entire structure onto the stack but if we use pass by reference that is if we pass a pointer to structure then only 4 bytes of memory is consumed. Thus with pass-by-reference, we tend to save memory.



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: