call by value and call by reference & Difference between Call by Value and Call by Reference

Hello friends! Today in this post we will read in full detail about Call by Value & Call by Reference in C in Hindi and will also see the difference between them. If you read it completely, you will understand it easily. So let’s start:-

Call by Value & Call by Reference in C

In the C programming language, a function can be called in two ways. First call by value and second call by reference.

call by value and call by reference
call by value and call by reference

Call By Value in C

In the Call by Value method, the values of the actual parameters are copied into the formal parameters of the function.

In other words, “in this the value of the variable is used in the function call.”

In Call by value method, we cannot change the value of actual parameter by using formal parameters.

In this, the actual and formal parameters are stored in different memory locations.

The actual parameter is an argument that is used in a function call while the formal parameter is an argument that is used in the function definition.

Its example-

Its program is given below:-

#include
#include

void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}

void main()
{
int x = 62, y = 82;
clrscr();
swap(x, y); // passing value to function
printf("\nValue of x: %d",x);
printf("\nValue of y: %d",y);
getch();
}

Its output –

Read Also:  how to use increment() and decrement() in laravel Eloquent? - laravel increment

Value of x: 62
Value of y: 82

Call By Reference in C

In Call by reference, the address of an argument is copied into the formal parameters.

In other words, “in this the address of the variable is passed to the function call as the actual parameter.”

Free Live Chat for Any Issue

In this, if i change or update the value of the formal parameter, then the value of the actual parameter will also change.

In call by reference, both the actual and formal parameters are stored in the same memory location.

example of –

Its program is given below:-

#include
#include

void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

void main()
{
int x = 62, y = 82;
clrscr();
swap(&x, &y); // passing value to function
printf("\nValue of x: %d",x);
printf("\nValue of y: %d",y);
getch();
}

example of –

Value of x: 82
Value of y: 62

Note:

  • Actual parameter – This is the argument which is used in the function call.
  • Formal parameter – This is the argument which is used in the function definition

Difference between Call by Value and Call by Reference

Following are the differences between them:-

Call By Value Call By Reference
While calling a function, when we pass the value of a variable, such functions are called call by value. While calling a function, when we pass the address of the variable So such functions are called call by reference.
In this if we change the copy of the variable then the original value of the variable does not change. If you change the value of the variable then the value of the variable outside the function will also change
In this we cannot change the value of the actual variable through function call. In this we can change the value of the actual variable by function call.
In this the values ​​of variables are passed by simple technique. Pointer variable is required to store the address of variables in this.
In this both actual and formal parameters are stored in different memory location. In this the actual and formal parameters are stored in the same memory location.
It can be done through C++, PHP, Visual Basic NET, and C# is supported. It is mainly supported by Java.
Read Also:  Laravel 7/6 disable registration example

Calling PHP function with the address of a variable instead of value is known as function call by reference.

call by value and call by reference in PHP

PHP allows you to call function by value and reference both. In call by reference, the address of a variable (their memory location) is passed.

Call By Value in PHP in PHP

<?php  
function increment($var){  
    $var++;  
    return $var;  
}  
$a = 5;  
$b = increment($a);  
echo $a; //Results: 5  
echo $b; //Results: 6   
?>   

Also Read : Pass By Value And Pass By Reference In Php

Read Also:  Create Dynamic Image Slider using PHP with jQuery

Call by Reference in PHP

<?php  
function increment(&$var){  
    $var++;  
    return $var;  
}  
$a = 5;  
$b = increment($a);  
echo $a; //Results: 6  
echo $b; //Results: 6   
?>  

In Call by value method original value is not modified whereas, in Call by reference method, the original value is modified.

Download