Passing by reference is a technique for passing parameters to a function. It is also known as call by reference, call by pointers, and pass by pointers. In this article, we will discuss this technique and how to implement it in our C program.
Pass By Reference in C
In this method, the address of an argument is passed to the function instead of the argument itself during the function call. Due to this, any changes made in the function will be reflected in the original arguments. We use the address operator (&) and indirection operator (*) to provide an argument to a function via reference.
When any argument is passed by reference in the function call as a parameter, it is passed using the address of(&) operator from the main function. In the function definition, the function takes the value of the parameter by using (*) operator. By using this, it can directly access the value of the parameter directly from its original location. There will be no need for extra memory or copying the same data into some other fields.
Also, a pointer type must be defined for the matching parameter in the called function in C.
Syntax
The code will look like this in the pass-by-reference method:
// function definion
return_type functionName (arg_type *arg1, arg_type *arg1, ......) {
// function body
}
.
.
// function call
functionName (&arg1, &arg2, ....);
Example of Pass by Reference
In the below program, the arguments that are passed by reference will be swapped with each other.
C
// C program to swap two numbers
#include <stdio.h>
// function definition with relevant pointers to recieve the
// parameters
void swap(int* a, int* b)
{
// accessing arguments like pointers
int temp = *a;
*a = *b;
*b = temp;
}
// driver code
int main(void)
{
int n1 = 5;
int n2 = 10;
// value before swapping
printf(" Before swapping : n1 is %d and n2 is %d\n", n1,
n2);
// calling the function by passing the address of the
// arguments
swap(&n1, &n2);
// value after swapping
printf(" After swapping : n1 is %d and n2 is %d\n", n1,
n2);
return 0;
}
Output Before swapping : n1 is 5 and n2 is 10
After swapping : n1 is 10 and n2 is 5
Explanation
In the above code, n1 and n2 are assigned by the values 5 and 10. When the swap function is called and the address of n1, and n2 are passed as a parameter to the function, then the changes made to the n1 and n2 in the swap function will also reflect in the main function. The swap function will directly access and change the values of n1, and n2 using their addresses. So, after execution of the swap function, the values of n1 and n2 are swapped.
Points to Remember
- When calling a function by reference, the actual parameter is the address of the variable that is being called.
- Since the address of the actual parameters is passed, the value of the actual parameters can be altered.
- Changes performed inside the function remain valid outside of it. By altering the formal parameters, the values of the real parameters actually change.
- Both actual and formal parameters refer to the same memory location.
Applications
- The pass-by-reference method is used when we want to pass a large amount of data to avoid unnecessary memory and time consumption for copying the parameters.
- It is used where we want to modify the actual parameters from the function.
- It is used to pass arrays and strings to the function.
Conclusion
Passing by reference in programming allows functions to directly access and modify the values of variables, making it efficient for large data and avoiding memory duplication. It's valuable for updating values and can be particularly useful when working with complex data structures.
Similar Reads
C++ Functions - Pass By Reference In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so
3 min read
Perl | Pass By Reference When a variable is passed by reference function operates on original data in the function. Passing by reference allows the function to change the original value of a variable. When the values of the elements in the argument arrays @_ are changed, the values of the corresponding arguments will also c
2 min read
References in C++ In C++, a reference works as an alias for an existing variable, providing an alternative name for it and allowing you to work with the original data directly.Example:C++#include <iostream> using namespace std; int main() { int x = 10; // ref is a reference to x. int& ref = x; // printing v
5 min read
Passing Map as Reference in C++ STL Prerequisite: Maps in C++ STL Pass by reference Elements in the map are in form of pairs where the first is key and another value, denoting key-value pairs. Also, all the key values are unique means no two elements can have the same key value. Passing maps by value is a costly task, costly in terms
3 min read
Pointers and References in C++ In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a
5 min read