difference of function call with reference or directly with variable

Hi guys,

during my homework, it appeared a question which I couldn't answer with my friends or with searching in google. I wrote a small example to get straight to the point.

We had to do an exercise with functions an call by reference. In the solution, there is always first created a reference to a variable, and the the function is called with the reference as a parameter. When I tried to solve the exercises, I always directly called the function with the variable as an argument. The function itself was absolutely the same compared to the solution.

As the example shows, both ways are working well I think. My Question now: Is there any reason, why I should create a reference first and give it to the function and NOT the variable itself? Is there any reason regarding security? What are the differences?

Since this is my first post, I hope that I described my problem well and fulfilled your requirements to answer my questions.

PS: Sorry for bad english, swiss guy here


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

void square(float& x)
{
    x = x*x;
}

int main(int argc, const char * argv[])
{
    float y = 0;
    
    cout << "Enter number: ";
    cin  >> y;
    
    float z = y;
    float &t = z;

    square(y);
    square(t);
    cout << "Squarenumber is: "                         << y << endl;
    cout << "Squarenumber with call by reference is: "  << z << endl;
    
    return 0;
}
There is no reason to create a reference variable for the sole purpose of passing it to the function. All references do is create an alias to the previously assigned variable. Thus, passing in the reference variable is doing the exact same thing as passing in the actual variable. Matter of fact, the compiler might actually replace the reference variable with the actual variable depending on the situation. References variables are generally used as a means of storing an alias to objects for further use in a structure or class of sorts.
I think your example should probably be more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

void squareByReference(float& x)
{
    x = x*x;
}

float squareByValue(float x)
{
    return x*x;
}

int main(int argc, const char * argv[])
{
    float y = 0;
    
    cout << "Enter number: ";
    cin  >> y;
    
    float z = y;
    z = squareByValue(y);
    squareByReference(y); // after this call has completed, the value of y has changed.
// So if we did these calls in the other order, z would become y^4 instead of y^2.

    cout << "Squarenumber with call by value is: " << z << endl;
    cout << "Squarenumber with call by reference is: "  << y << endl;
    
    return 0;
}


Using a by-reference-function-call, you can create functions that can return more than one variable. A by-value-function-call can take any number of arguments, but it can return only one value.

If you want to, you can also combine the two. This is often done to create functions that return an error-code (by value) and one or more results (by reference).
Last edited on
Topic archived. No new replies allowed.