Pass by Reference in C

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.

Problem Description

Write a C program to demonstrate pass by reference.

Problem Solution

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.

advertisement
advertisement
Method 1: Swapping Numbers using Pass by Reference in C

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.

Pass by Reference in C Program/Source Code

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;
}
Program Explanation

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).

advertisement
Program Output

“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

Method 2: Find the Cube of a Number in C using Pass by Reference

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).

advertisement

When a function is called, its address is passed to the function, which is then accessed using pointers.

Pass by Reference in C Program/Source Code

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.

  1. /*
  2.  * C program to demonstrate pass by reference by finding the cube of a number
  3.  */
  4. #include <stdio.h>
  5.  
  6. void cube(int *a)
  7. {
  8.     //*(asterix) is used to de-reference the variable
  9.     *a=(*a)*(*a)*(*a); // finding cube of a number
  10. }
  11. int main() 
  12. {
  13.     int a;
  14.     printf("Enter a number:");
  15.     scanf("%d",&a);
  16.     printf("Numbers Entered is: %d\n",a);
  17.  
  18.     // Calling cube function by passing address
  19.     cube(&a);
  20.     printf("Cube of entered number is: %d",a);
  21.     return 0;
  22. }
Program Explanation

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).

Runtime Test Cases
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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.