When Do We Pass Arguments by Reference or Pointer in C++?
In C++, we can pass arguments to a function as a value, reference (or pointer). Each method has its unique benefits and uses. In this article, we will discuss the application or cases where we should pass the arguments by reference or a pointer. But first, let’s quickly revisit the definitions of pointers and references.
Pointers in C++
Pointers are symbolic representations of addresses. They are the variables that store the memory address of the data. The address of the variable you’re working with is assigned to the pointer variable that points to the same data type (such as an int or string).
We can use the pointers to pass arguments address to the function i.e. pass by pointer method.
Syntax to Declare Pointer
data_type * pointer_name = &some_variable;
To learn more about pointers, visit the article – C++ Pointers
References in C++
When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration. They are similar to pointers in the way that ‘&’ is used for signifying the address of a variable or any memory. The difference is that we don’t need to use *(dereference operator) to access the value of the reference. We can use the reference name to directly access the value.
Syntax to Declare References
data_type &ref = variable;
To learn more about pointers, visit the article –C++ References
When to Pass Arguments by Reference or Pointer in C++?
In C++, arguments are passed by reference or pointer in the following cases:
1. To Modify the Local Variable of the Caller Function
A reference (or pointer) allows a called function to modify the local variables of the caller function that are passed as the arguments. This means that a function can modify a variable in the calling function by using a pointer to that variable.
Example
C++
// C++ program to modify local variables of the caller // function #include <iostream> using namespace std; // function to modify local variable a void fun( int * ptr) { *ptr = 4; } // driver code int main() { int num = 2; cout << "Old value of a is " << num << endl; fun(&num); cout << "New value of a is " << num << endl; return 0; } |
Old value of a is 2 New value of a is 4
2. Avoiding Copy of Large Data
Imagine a function that has to receive a large-sized object, if we pass it without reference, a new copy of it is created which causes a waste of CPU time and memory. We can use references or pointers to avoid this.
Example
C++
// C++ program to show how to pass the structure as a // reference to a function #include <iostream> using namespace std; // Structure to represent Intern struct Intern { string name; string company; }; // Function to print Intern details void printInDetails( const Intern& it) { cout << "Name: " << it.name << endl; cout << "Company: " << it.company << endl; } int main() { // Creating an instance of Intern Intern it1 = { "Abhishek" , "GeekforBeginner" }; printInDetails(it1); return 0; } |
Name: Abhishek Company: GeekforBeginner
3. To Achieve Run Time Polymorphism in a Function
We can make a function polymorphic by passing objects as references (or pointers) to it instead of the actual objects. It allows us to achieve runtime polymorphism in our program.
Example:
C++
#include <iostream> using namespace std; // Define the base class structure struct Base { virtual void show() { cout << "In base\n" ; } }; // Define the derived class structure struct Derived : public Base { // Override for Derived behavior void show() override { cout << "In derived\n" ; } }; // Function to print using polymorphism void print(Base* base) { base->show(); } // driver code int main( void ) { Base b; Derived d; print(&b); print(&d); return 0; } |
In base In derived
4. To Return Multiple Values
When we want to return multiple values from a function, passing pointers as function parameters allows us to modify the values of variables passed to the function.
Example:
C++
#include <iostream> using namespace std; // Function to calculate sum and difference of two numbers void calcSumAndDifference( int x, int y, int * sum, int * diff) { *sum = x + y; *diff = x - y; } int main() { int x = 4; int y = 2; int sum, diff; // calling the function to calculate sum and difference calcSumAndDifference(x, y, &sum, &diff); // printing the sum and difference returned from function cout << "Sum is: " << sum << endl; cout << "Difference is: " << diff << endl; return 0; } |
Sum is: 6 Difference is: 2
Contact Us