What is Pass by Reference in C?
In pass by reference, we pass the address of the variable instead of passing the value of the variable and access the variable using the pointers in the Function. All the changes made to variable in the function will be reflected in the main Program.
Example:
Consider Pass by reference as google drive and Call by value as bluetooth system. If you receive a file via bluetooth, any changes made to that file will only affect your file and not the main file, which is the core concept of call by reference, whereas any changes made to a file in Google Drive will be reflected everywhere, which is the main ideology behind pass by reference.
Write a C program to demonstrate pass by reference.
1. Pass the addresses of the variables as parameters to the function.
2. In function definition receive the parameters through pointers.
3. Print the output and exit.
Let’s take a look at pass by reference in C using two examples.
- Swapping Numbers using Pass by Reference in C
- Find the Cube of a Number in C using Pass by Reference
In this approach, we declare a function that swaps two numbers and takes a pointer to a variable as a parameter, so that any changes to the variable in the function are reflected everywhere.
Here is source code of the C Program to illustrate pass by reference by swapping two numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Illustrate Pass by Reference by Swapping Numbers */ #include <stdio.h> void swap(int *a,int *b) { //*(asterix) is used to de-reference the variable int temp=*a; *a=*b; *b=temp; } int main() { int a,b; printf("Enter two numbers:"); scanf("%d %d",&a,&b); printf("Numbers Before Swapping: %d and %d\n",a,b); //Calling swap function by passing address swap(&a,&b); printf("Numbers After Swapping: %d and %d",a,b); return 0; }
1. Start the program.
2. Take two numbers as input.
3. Declare a function to swap two numbers which takes a pointer to a variable as parameter i.e any changes to variable in the function will be reflected everywhere.
4. Swap the two numbers inside the function using de-reference operator.
5. Now, call the function by passing the address of variables.
5. Print numbers before swapping and after swapping.
6. End the Program.
Example:
Input: a = 1, b = 2;
Output: a = 2, b = 1;
Let’s understand an example with detailed steps.
Given two numbers, a=1 and b=2.
1. We will assign a value to temp variable i.e. temp = 1.
2. After that we will assign b to a => b = a i.e. a = 2.
3. Assign temp to b => b = temp i.e b = 1.
Time Complexity: O(1)
In a pass by reference program, only basic input/output and swapping operations are performed, which takes O(1) time.
Space Complexity: O(1)
Since no auxiliary space is required, time complexity is O(1).
“1” and “2” as input for number swapping using pass by reference in C.
Enter two numbers: 1 2 Numbers Before Swapping: 1 and 2 Numbers After Swapping: 2 and 1
In this method, we declare a function to find the cube of a number that accepts a pointer to a variable as a parameter and call it by passing the variable’s address.
Function prototype of pass by reference is like: int cube(int *a);
And the function is called in the following fashion: cube(&a).
When a function is called, its address is passed to the function, which is then accessed using pointers.
Here is source code of the C Program to illustrate pass by reference by finding the cube of a number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to demonstrate pass by reference by finding the cube of a number
*/
#include <stdio.h>
void cube(int *a)
{
//*(asterix) is used to de-reference the variable
*a=(*a)*(*a)*(*a); // finding cube of a number
}
int main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
printf("Numbers Entered is: %d\n",a);
// Calling cube function by passing address
cube(&a);
printf("Cube of entered number is: %d",a);
return 0;
}
1. First input a number and print the entered number.
2. Declare a function to find a cube of a number which takes a pointer to a variable as parameter i.e any changes to variable in the function will be reflected everywhere.
3. Find the cube of number inside the function using de-reference operator.
4. Call the function by passing the address of the variable.
5. Print the cube of number.
Example:
Enter a number: 3
Cube of entered number is: 27
cube of 3 is (3*3*3) = 27.
Time Complexity: O(1)
In a pass by reference program, only basic input/output and swapping operations are performed, which takes O(1) time.
Space Complexity: O(1)
Since no auxiliary space is required, time complexity is O(1).
Enter a number: 3 Numbers Entered is: 3 Cube of entered number is: 27
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Apply for C Internship
- Practice BCA MCQs
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Check C Books