Call By Value v/s Call By Reference

Call by value and Call by reference in C

Call by value and Call by reference in C:

Please find below details about Call by value and Call by reference in C –

There are two methods to pass the data into the function in C language, i.e., call by value and call by reference.

call by value and call by reference in c

Let’s understand call by value and call by reference in c language one by one.

Call by value in C:

  • In call by value method, the value of the actual parameters is copied into the formal parameters. In other words, we can say that the value of the variable is used in the function call in the call by value method.
  • In call by value method, we can not modify the value of the actual parameter by the formal parameter.
  • In call by value, different memory is allocated for actual and formal parameters since the value of the actual parameter is copied into the formal parameter.
  • The actual parameter is the argument which is used in the function call whereas formal parameter is the argument which is used in the function definition.

Let’s try to understand the concept of call by value in c language by the example given below:

#include<stdio.h>

void change(int num) {

printf(“Before adding value inside function num=%d \n”,num);

num=num+100;

printf(“After adding value inside function num=%d \n”, num);

}

int main() {

int x=100;

printf(“Before function call x=%d \n”, x);

change(x);//passing value in function  

printf(“After function call x=%d \n”, x);

return 0;

}

Output

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100

Call by Value Example: Swapping the values of the two variables

#include <stdio.h>

void swap(int , int); //prototype of the function 

int main()

{

int a = 10;

int b = 20;

printf(“Before swapping the values in main a = %d, b = %d\n”,a,b); // printing the value of a and b in main

swap(a,b);

printf(“After swapping values in main a = %d, b = %d\n”,a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20

}

void swap (int a, int b)

{

int temp;

temp = a;

a=b;

b=temp;

printf(“After swapping values in function a = %d, b = %d\n”,a,b); // Formal parameters, a = 20, b = 10 

}

Output

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20  

Call by reference in C:

  • In call by reference, the address of the variable is passed into the function call as the actual parameter.
  • The value of the actual parameters can be modified by changing the formal parameters since the address of the actual parameters is passed.
  • In call by reference, the memory allocation is similar for both formal parameters and actual parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.

Consider the following example for the call by reference.

#include<stdio.h>

void change(int *num) {

printf(“Before adding value inside function num=%d \n”,*num);

(*num) += 100;

printf(“After adding value inside function num=%d \n”, *num);

}

int main() {

int x=100;

printf(“Before function call x=%d \n”, x);

change(&x);//passing reference in function  

printf(“After function call x=%d \n”, x);

return 0;

}

Output

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
Call by reference Example: Swapping the values of the two variables

#include <stdio.h>

void swap(int *, int *); //prototype of the function 

int main()

{

int a = 10;

int b = 20;

printf(“Before swapping the values in main a = %d, b = %d\n”,a,b); // printing the value of a and b in main

swap(&a,&b);

printf(“After swapping values in main a = %d, b = %d\n”,a,b); // The values of actual parameters do change in call by reference, a = 10, b = 20

}

void swap (int *a, int *b)

{

int temp;

temp = *a;

*a=*b;

*b=temp;

printf(“After swapping values in function a = %d, b = %d\n”,*a,*b); // Formal parameters, a = 20, b = 10 

}

Output

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10  

Difference between call by value and call by reference in c

No. Call by value Call by reference
1 A copy of the value is passed into the function An address of value is passed into the function
2 Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters. Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters.
3 Actual and formal arguments are created at the different memory location Actual and formal arguments are created at the same memory location

Related Post:

Features of C Programming Language

Variables in C

Data Types in C

Keywords in C

C Operators

Comments in C

Escape Sequence in C

C Functions

Storage Classes in C

Dynamic memory allocation in C

Factorial Program in C

Leap year program in C

Fibonacci Series in C

Prime Number program in C

Palindrome program in C

Sum of digits program in C

Escape Sequence in C

ASCII value in C

Difference Between Type Casting and Type Conversion in C

Difference Between Variables and Constants

Matrix multiplication in C

C Program to generate Fibonacci Triangle

C Program to print “hello” without semicolon

C Program to swap two numbers without third variable

C Program to reverse number

Count the number of digits in C

Tokens in C

C Identifiers

C Strings

Compile time vs Runtime in C

C break statement

C goto statement

Type Casting in C

C String Functions

C Pointers

Dangling Pointers in C

void pointer in C

Pointer to Pointer in C

Recursion in C

File Handling in C

C fprintf() and fscanf()

C fputc() and fgetc()

C fputs() and fgets()

C fseek() function

Constant Pointers in C