Pass by Reference in C++ language

Pass by Reference in C++ language

The default technique for passing parameters to functions is pass by value . C++ also supports pass by reference, also known as call by reference.

Reference Variables in c++ language AndroWep-tutorials
Reference Variables in c++ language AndroWep-tutorials
#include <iostream>
/*
* swap(a, b)
* Attempts to interchange the values of
* its parameters a and b. That it does, but
* unfortunately it only affects the local
* copies.
*/

void swap(int a, int b) 
{
    int temp = a;
    a = b;
    b = temp;
}

/*
* main
* Attempts to interchange the values of
* two variables using a faulty swap function.
*/

int main() 
{
    int var1 = 5, var2 = 19;
    std::cout << "var1 = " << var1 << ", var2 = " << var2 << '\n';
    swap(var1, var2);
    std::cout << "var1 = " << var1 << ", var2 = " << var2 << '\n';
}
var1 = 5, var2 = 19
var1 = 5, var2 = 19

We really would like to write a function that interchanges the caller’s variables.

Pass by Reference via Pointers

Pointers allow us to access the memory locations of variables. We can use this capability to allow a function to modify the values of variables that are owned by its caller. provides a correct version of our variable interchange program.

#include <iostream>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
/*
* main
* Interchanges the values of two variables
* using the swap function.
*/
int main() {
int var1 = 5, var2 = 19;
std::cout << "var1 = " << var1 << ", var2 = " << var2 << '\n';
swap(&var1, &var2);
std::cout << "var1 = " << var1 << ", var2 = " << var2 << '\n';
}
var1 = 5, var2 = 19
var1 = 19, var2 = 5

The formal parameters to swap, a and b, are pointers to integers; they are not integers themselves. In order to access the integer to which the pointer named a refers, it must be dereferenced. That is why any use of a in swap’s body is prefixed with the pointer dereferencing operator, *. The statement

int temp = *a;

In swap’s statement

*a = *b;
*b = temp;
//from main call
swap(&var1, &var2);

Pass by Reference via References

#include <iostream>
/*
* swap(a, b)
* Interchanges the values of memory
* referenced by its parameters a and b.
* It effectively interchanges the values
* of variables in the caller's context.
*/
void swap(int& a, int& b) 
{
    int temp = a;
    a = b;
    b = temp;
}
/*
* main
* Interchanges the values of two variables
* using the swap function.
*/
int main() 
{
    int var1 = 5, var2 = 19;
    std::cout << "var1 = " << var1 << ", var2 = " << var2 << '\n';
    swap(var1, var2);
    std::cout << "var1 = " << var1 << ", var2 = " << var2 << '\n';
}

The formal parameters to swap, a and b, are references to integers; this is signified by the & symbol following int in their declarations. Because a is a reference we use it exactly like an integer; there is no need to dereference it with the * operator to change the value of the integer it aliases. Because a is a reference, however, it is an alias to another variable or memory location elsewhere. This means if we modify a, we also modify the variable it references, in this case var1 in main. The statement

int temp = a;
//Call from main function 
swap(var1, var2);
#include <iostream>
int add(int x, int y)
{
    return x + y;
}
int multiply(int x, int y) 
{
    return x * y;
}
int evaluate(int (*f)(int, int), int x, int y)
{
    return f(x, y);
}
int main() 
{
    std::cout << add(2, 3) << '\n';
    std::cout << multiply(2, 3) << '\n';
    std::cout << evaluate(&add, 2, 3) << '\n';
    std::cout << evaluate(&multiply, 2, 3) << '\n';
}

C++ achieves higher-order functions via function pointers. During a program’s execution, the compiled machine language code for a function must reside in the computer’s memory for the program to be able to invoke the function. That means every function has a memory address just as each variable has its own specific memory address. A pointer to a function holds the starting address for the compiled code of a particular function.