Introduction

This article will cover how to use pass-by-reference in C++ and give some examples of how it can be used. And what is the difference between pass-by-value and pass-by-reference in the C++ function?

Pass by value in C++

Pass by value is a type of parameter passing in a programming language where the called function does not need to know about the contents of its parameter.

From pass-by-value to pass-by-pass by reference in C++

Passing by reference is a technique used in C++ programming. It allows the passing of objects by reference rather than the default behavior of passing them by value.

The difference between pass-by variables and pass-by-reference

Pass-by variables are variables that are passed to a function by value. This means that the variable is copied and passed to the function, so any changes in the variable do not affect the original variable.

Pass-by-reference is when a variable is passed to a function by reference, meaning any changes made to the variable inside the function will be reflected in the original.

Pass by reference syntax in C++

To pass by reference, we use the “&” operator. For example:

void exchange(int &s, int &h){int d = s;s = h;h = d;}

Example 1

We call the exchange function, which will change the values of random_number1 and random_number2.

pass by reference example 1

The code of the program

#include<iostream>#include<vector>using namespace std;void exchange(int &s, int &h){int d = s;s = h;h = d;}int main() {int random_number1= 17;int random_number2 = 43;cout << “first version: ” << endl;cout << random_number1 << random_number2 << endl;exchange(random_number1, random_number2);cout<<endl;cout << “second version: ” << endl;cout << random_number1 << random_number2 << endl;return 0;}

The output of the program

output pass by refference 1

Example 2

The following example shows the difference between passing by value and passing by reference.

pass by reference example 2

The code of the program

#include<iostream>#include<vector>using namespace std;void pass_by_value(int number) {number += 27;}void pass_by_reference(int &number) {number += 27;}int main() {int first_number = 7, second_number = 7;cout << “Before: first_number = ” << first_number << “, second_number = ” << second_number << endl;cout<< endl;pass_by_value(first_number);pass_by_reference(second_number);cout << “After: first_number = ” << first_number << “, second_number = ” << second_number << endl;return 0;}

The output of the program

output pass by refference 2

example 3

pass by reference example 3

The code of the program

#include<iostream>#include<vector>using namespace std;void Sqrt_function(int& b) { b*=b;} int main() {int h = 21; cout << “The first version of h is ” << h << endl; cout<<endl;Sqrt_function(h); cout << “The second version of h is ” << h << endl; return 0;}

The output of the program

output pass by refference 3

Conclusion

conclusion of the article

In conclusion, passing by reference in C++ is a very useful and efficient way to pass data between two different objects. It helps reduce the number of lines of code in a program and improves the program’s efficiency. Passing by reference is a technique that can be used to optimize the performance of a program. It can also be used to avoid unnecessary copies and make the code easier to read.

Pin It on Pinterest

Share This