in , ,

C++ Call by Reference: Using pointers [With Examples]

C++ Call by Reference: Using pointers [With Examples]
C++ Call by Reference: Using pointers [With Examples]

C++ Call by Reference: Using pointers [With Examples]

In this tutorial, we will find out about C++ call by reference to pass pointers as a contention to the capacity with the help of examples.

In the C++ Functions tutorial, we found out about passing contentions to a capacity. This strategy used is called passing by value on the grounds that genuine worth is passed.

In any case, there is another method of passing contentions to a capacity where the real estimations of contentions are not passed. Rather, the reference to values is passed.

For instance,

// function that takes value as parameter

void func1(int numVal) {
    // code
}

// function that takes reference as parameter
// notice the & before the parameter
void func2(int &numRef) {
    // code
}

int main() {
    int num = 5;

    // pass by value
    func1(num);

    // pass by reference
    func2(num);

    return 0;
}

Notice the & in void func2(int &numRef). This denotes that we are using the address of the variable as our parameter.

Along these lines, when we call the func2() work in main() by passing the variable num as a contention, we are in reality passing the location of num variable rather than the value 5.


Example 1: Passing by reference without pointers

#include <iostream>
using namespace std;

// function definition to swap values
void swap(int &n1, int &n2) {
    int temp;
    temp = n1;
    n1 = n2;
    n2 = temp;
}

int main()
{

    // initialize variables
    int a = 1, b = 2;

    cout << "Before swapping" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    // call function to swap numbers
    swap(a, b);

    cout << "\nAfter swapping" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

Output

Before swapping
a = 1
b = 2

After swapping
a = 2
b = 1

In this program, we passed the variables a and b to the swap() function. Notice the function definition,

void swap(int &n1, int &n2)

Here, we are using and to indicate that the capacity will acknowledge addresses as its parameters.

Henceforth, the compiler can recognize that rather than real qualities, the reference of the factors is passed to work parameters.

In the swap() work, the capacity parameters n1 and n2 are are pointing to the same value as the variables a and b respectively. Hence the swapping takes place on actual value.

The same task can be done using the pointers. To learn about pointers, visit C++ Pointers.


Example 2: Passing by reference using pointers

#include <iostream>
using namespace std;

// function prototype with pointer as parameters
void swap(int*, int*);

int main()
{

    // initialize variables
    int a = 1, b = 2;

    cout << "Before swapping" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    // call function by passing variable addresses
    swap(&a, &b);

    cout << "\nAfter swapping" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    return 0;
}

// function definition to swap numbers
void swap(int* n1, int* n2) {
    int temp;
    temp = *n1;
    *n1 = *n2;
    *n2 = temp;
}

Output

Before swapping
a = 1
b = 2

After swapping
a = 2
b = 1

Here, we can see the output is the same as the previous example. Notice the line,

// &a is address of a
// &b is address of b
swap(&a, &b);

Here, the address of the variable is passed during the capacity call as opposed to the variable.

Since the location is passed rather than esteem, a dereference operator* must be used to access to the worth put away in that address.

temp = *n1;
*n1 = *n2;
*n2 = temp;

*n1 and *n2 gives the value stored at address n1 and n2 respectively.

Since n1 and n2 contain the addresses of a and b, anything is done to *n1 and *n2 will change the actual values of a and b.

Hence, when we print the values of a and b in the main() function, the values are changed.


Thanks for watching! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

How to Make Business Cards

How To Make Business Cards in CorelDRAW

How to Make a Monogram

How to Make a Monogram in CorelDRAW