Table of Contents
What is call by reference in C ?
In this type of function calling the address of an argument is passed to formal parameters of the function.
Function has pointer variables in the parameters which accept address of argument variables.
During operation these addresses are used to access the argument and if we made changes on these addresses then it will get reflected in argument variables.
Which means unlike call by value here argument value will get altered accordingly because of operation of function.
Example of call by reference in C
swap() in below code has two parameters a and b.
These parameters will accept addresses of x and y.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main ()
{
int x = 5;
int y = 8;
printf("Value of x before swapping : %dn", x );
printf("Value of y before swapping : %dn", y );
swap(&x,&y); // passing the addresses of x and y
printf("Value of x before swapping : %dn", x );
printf("Value of y before swapping : %dn", y );
return 0;
}
Output :
Value of x before swapping : 5
Value of y before swapping : 8
Value of x before swapping : 8
Value of y before swapping : 5
As we can see in the above output the function’s operation will affect values of argument variables.
Because the function has performed swap operations on addresses of both the arguments x and y.
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM
