blog icon indicating copy to clipboard operation
blog copied to clipboard

C++ Using Reference Variables as Function Parameters

Open qingquan-li opened this issue 2 years ago • 0 comments

Concept: When used as parameters, reference variables allow a function to access the parameter's original argument. Changes to the parameter are also made to the argument.

A reference variable is an alias for another variable.

Reference variables are defined like regular variables, except you place an ampersand (&) in front of the number.

Example01:

#include <iostream>

using namespace std;

void addOne(int&);

int main() {
    int y = 1;
    cout << "In main function, y is " << y << endl;
    addOne(y);
    cout << "After executing `addOne(y);` y is now " << y << endl;
    return 0;
}

// The variable x is called "a reference to an int."
void addOne(int& x) {
    x++;
}

Output:

In main function, y is 1
After executing `addOne(y);` y is now 2

The parameter x "points" to the y variable in function main.

When a program works with a reference variable, it is actually working with the variable it references, or to which it points.

Recall that function arguments are normally passed by value, which means a copy of the argument's value is passed into the parameter variable. When a reference parameter is used, it is said that the argument is passed by reference.


Example02:

Function parameters passed by value (copy):

#include <iostream>

using namespace std;

void max_min(float, float);

int main() {
    float larger, smaller;
    cout << "Type in two values for max and min: ";
    cin >> larger >> smaller;
    max_min(larger, smaller);
    cout << "The larger number is: " << larger << endl;
    cout << "The smaller number is: " << smaller << endl;
    return 0;
}

void max_min(float max, float min) {
    float temp;
    if (max < min) {
        temp = max;
        max = min;
        min = temp;
    }
    cout << "max: " << max << ", min: " << min << endl;
}

Output:

Type in two values for max and min: 100 200
max: 200, min: 100
The larger number is: 100
The smaller number is: 200

Function parameters passed by reference:

#include <iostream>

using namespace std;

void max_min(float&, float&);

int main() {
    float larger, smaller;
    cout << "Type in two values for max and min: ";
    cin >> larger >> smaller;
    max_min(larger, smaller);
    cout << "The larger number is: " << larger << endl;
    cout << "The smaller number is: " << smaller << endl;
    return 0;
}

void max_min(float& max, float& min) {
    float temp;
    if (max < min) {
        temp = max;
        max = min;
        min = temp;
    }
}

Output:

Type in two values for max and min: 100 200
The larger number is: 200
The smaller number is: 100

Example03:

#include <iostream>

using namespace std;

void winner_loser(float, float, float&, float&);

int main() {
    float p1_score, p2_score, win_score, lose_score;
    cout << "What is player1's score? ";
    cin >> p1_score;
    cout << "What is player2's score? ";
    cin >> p2_score;

    winner_loser(p1_score, p2_score, win_score, lose_score);

    cout << "The winner's score was: " << win_score << endl;
    cout << "The loser's score was: " << lose_score << endl;

    return 0;
}

void winner_loser(float player1_score, float player2_score, float& winner_score, float& loser_score) {
    if (player1_score > player2_score) {
        winner_score = player1_score;
        loser_score = player2_score;
    }
    else {
        winner_score = player2_score;
        loser_score = player1_score;
    }
}

Output:

What is player1's score? 100
What is player2's score? 200
The winner's score was: 200
The loser's score was: 100

Example04:

// This program asks the user to enter the diameter of a circle,
// and then calculates the circumference of the circle.

#include <iostream>

using namespace std;

// Function prototypes
double getCircumference(double);
void setRadius(double, double&);

// Global variable
const double PI = 3.14159265;

int main() {
    double diameter, radius, circumference;

    cout << "What is the diameter (diameter must be greater than 0)? ";
    cin >> diameter;

    // Get the value of the radius
    setRadius(diameter, radius);

    // Print the radius
    cout << "The radius is " << radius << endl;

    // Assign the value (circumference) to the variable circumference
    circumference = getCircumference(radius);

    cout << "The circumference of the circle is " << circumference << endl;

    return 0;
}

// Calculates and returns the circumference of the circle.
double getCircumference(double circle_radius) {
    double circle_circumference;
    circle_circumference = 2 * PI * circle_radius;
    return circle_circumference;
}

// Has 2 parameters: the diameter which is passed by value
// and a radius which is passed by reference.
// Based on the diameter, the function will assign a value to the variable
// which radius references. (radius is half of the diameter.)
void setRadius(double circle_diameter, double& circle_radius) {
    circle_radius = circle_diameter / 2;
}

Output:

What is the diameter (diameter must be greater than 0)? 10
The radius is 5
The circumference of the circle is 31.4159

qingquan-li avatar Jun 23 '22 20:06 qingquan-li