Pass by Reference in C++

bookmark

#include <iostream>
 
void passByReference(int& data)
{
    std::cout << "Value of data in passByReference( ) is "
              << data << std::endl;
    data = 20;
    std::cout << "Value of data is changed now to "
              << data << std::endl;
    return;
}
 
int main()
{
    int data = 10;
 
    std::cout << "Value of data is "
          << data << std::endl;
    /* Variable data passed by reference */
    passByReference(data);
    /* Value of data is now 20 */
    std::cout << "Value after calling passByReference( ) is "
              << data;
    return 0;
}

 

Output:
Value of data is 10
Value of data in passByReference( ) is 10
Value of data is changed now to 20
Value after calling passByReference( ) is 20