[go: up one dir, main page]

0% found this document useful (0 votes)
30 views6 pages

Call by Value and Call by Reference

The document explains two methods of passing data to functions in C: call by value and call by reference. Call by value copies the actual parameter's value to the formal parameter, preventing modifications to the original variable, while call by reference passes the variable's address, allowing modifications to the original variable. It also provides examples and highlights the differences between the two methods regarding memory allocation, safety, and the ability to alter values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Call by Value and Call by Reference

The document explains two methods of passing data to functions in C: call by value and call by reference. Call by value copies the actual parameter's value to the formal parameter, preventing modifications to the original variable, while call by reference passes the variable's address, allowing modifications to the original variable. It also provides examples and highlights the differences between the two methods regarding memory allocation, safety, and the ability to alter values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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 in C

● In the call by value method, the value of the actual parameters is copied into the
formal parameters.

● In the 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
the formal parameter is the argument which is used in the function definition.

● Call by value method copies the value of an argument into the formal

parameter of that function. Therefore, changes made to the parameter


of the main function do not affect the argument.

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

#include<stdio.h>

void swap(int a, int b); //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);

swap(a,b); //function call

printf("After swapping values in main a = %d, b = %d\n",a,b);

void swap(int a, int b) //function definition //a=10 //b=20

int temp;

temp = a; //temp=10

a=b; //a=20
b=temp; //b=10

printf("After swapping values in function a = %d, b = %d\n",a,b);

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.

While calling a function, instead of passing the values of variables, we pass address
of variables(location of variables) to the function known as “Call By References.

// C program to swap 2 numbers by Call by Reference

#include <stdio.h>

// Function Prototype
void swapx(int* x, int* y);

// Main function

int main()

int x = 10, y = 20; //x = 6005 =10 //y=6010 =20

printf("Before swapping in main:x=%d y=%d\n", x, y);

// Pass reference

swapx(&x, &y); //function call

printf("After swapping in main:x=%d y=%d\n", x, y);

return 0;

void swapx(int* x, int* y) //*x=6005 *y=6010

int t;

t = *x; //t=10

*x = *y; //*x=20

*y = t; //*y=10

printf("After swapping in this swapx function:x=%d y=%d\n", *x, *y);

}
Output:

Before swapping in main:x=10 y=20

After swapping in this swapx function:x=20 y=10

After swapping in main:x=20 y=10

--------------------------------

Differences:-

Paramete Call by value Call by reference


rs
Definition While calling a function, While calling a function, in
when you pass values by programming language instead of
copying variables, it is copying the values of variables, the
known as “Call By address of the variables is used; it
Values.” is known as “Call By References.
Argument In this method, a copy of In this method, a variable itself is
s the variable is passed. passed.

Effect Changes made in a copy Change in the variable also affects


of a variable never modify the value of the variable outside the
the value of the variable function.
outside the function.
Alteration Does not allow you to Allows you to make changes in the
of value make any changes in the values of variables by using
actual variables. function calls.
Passing of Values of variables are Pointer variables are required to
variable passed using a store the address of variables.
straightforward method.
Value Original value not The original value is modified.
modificatio modified.
n
Memory Actual and formal Actual and formal arguments
Location arguments will be created in the same
will be created in different memory location
memory location
Safety Actual arguments remain Actual arguments are not
safe as they cannot be Safe. They can be
modified accidentally modified, so you need
accidentally. to handle arguments operations
carefully.

You might also like