Call by the reference in C++

Whenever a function is defined in a program written in the C++ language. If we want to call the function, it can be done in two ways that are:

  • Call by value
  • Call by reference

Before discussing the call-by-reference method, we will know about both ways to call the function

Call by value:

Whenever we pass the arguments or variables to the function in the call-by-value method, a copy of the actual parameters is sent to the function. So, if there is some manipulation in the function on the variables, it will not affect the actual values. So, the variables cannot be changed.

Call by reference

Here, the address of the actual variables is sent to the function, so whatever changes are made to the parameters or variables in the function will reflect on the actual variables. It can be achieved in C++ in two ways that are:

  • Passing the references to the variables
  • Achieving the pass-by-reference by using the pointers

Pass by reference in Cpp:

In this method, if we send the variables to the function using the references, the changes made to the variables in the function will be reflected in the actual values.

Example:

An example program to illustrate the call by reference in C++:

Output:

Call by the reference in C++

Explanation:

In the above program, there are two functions: one is the main function, and the other is the swapping function. The "main" function has two variables. First, we printed the two variables before passing the two variables to the swapping function. After that, we passed the variables to the swapping function, which swapped the two variables. We printed the two variables again after calling the function, and now the variables are swapped. It is the perfect example of the call by reference in C++. Here, the changes made to the two variables in the function are reflected in the actual variables.

Pass by reference using the pointers:

In this method, we pass the pointers of the variables or objects to the function instead of passing the actual variables or the actual objects, and it will also show the same effect as the above method. Here, the changes made to the variables inside the function will be reflected in the actual variables.

Example:

An example program to illustrate the call by reference using pointers in C++:

Output:

Call by the reference in C++

Explanation:

In the above program, there are 4 functions: that are add() function, sub() function, mul() function, and main function. The add() function will add the two variables and store the resultant in the res variable, the sub() function will subtract the second variable from the first variable and store the resultant in the res variable, and the mul() function will multiply the two variables and store the resultant in the res variable.

Here, the res variable and other variables are passed to the respective function through the pass-by-reference method using the pointers. In the main function, the variables are declared, a and b variables are taken from the user, and the res variables are initialized to zero. So whenever it is passed to functions, the changes are reflected on the res variable.


Next TopicFstream in C++




Latest Courses