Passing Values

Recommended Posts

Hello, As you may know, Im working with functions, and I want to make something clear. In the following code, is the function bonus(), it seems that it takes the value of points from main(), but dont you need a &'s when getting the value from another function?
#include <iostream>
using namespace std;

int bonus(int points);

int main()
{
int points = 100;
cout << "Your points: " << bonus(points);
system("pause");
return 0;
}

inline int bonus(int points)
{
return (points-10);
}



Share on other sites
The & denotes that you are passing by reference, in which case you are basically creating an alias for a variable defined elsewhere, so that if you change that value in your function, that value is also changed in the area from which you called the function.

Without the &, you are simply passing by value, in which case, a copy of the variable is made and sent to the function. Any changes to that copy will be local.

Edit: Thats a little unclear, look here for more information on references.

Share on other sites
In the example code, 'bonus(points)' *returns* a value, which is used for output. The passed-in parameter 'points' is not changed, because it was *passed by value*. By declaring (and then implementing) 'bonus' as 'int bonus(int& points);', you would specify passing by reference, which could then allow 'points' (as seen in main()) to be changed. (Here, though, you don't modify the input parameter within bonus(), so it still wouldn't change.)

Share on other sites
Quote:
 Original post by Driv3MeFarThe & denotes that you are passing by reference, in which case you are basically creating an alias for a variable defined elsewhere, so that if you change that value in your function, that value is also changed in the area from which you called the function.Without the &, you are simply passing by value, in which case, a copy of the variable is made and sent to the function. Any changes to that copy will be local.Edit: Thats a little unclear, look here for more information on references.

Ohhhhh ok, so because there is no declaration of points within the bonus function, it looks up one scope and find points is = to 100, it uses that value, correct?

Share on other sites
Quote:
 Original post by NUCLEAR RABBITOhhhhh ok, so because there is no declaration of points within the bonus function, it looks up one scope and find points is = to 100, it uses that value, correct?

Not quite. When you call the function in main and pass in points, a copy of that variable is made for the function bonus. Bonus now has its own variable, points, with a value of 100.

When passing by reference (with &), you're basically saying "Okay bonus, theres a variable here in main called points. Use that.", whereas when passing by value, bonus gets its very own copy of that variable, local only to the bonus function.

Maybe this will help:
void foo(int i);void bar(int &i);int main(){int i = 0;foo(i);std::cout<<i; //since foo changed only a local copy of i, this will still print out 0bar(i);std::cout<<i; //i was passed by reference to bar, so now this will print out 1return 0;}void foo(int i) //this is passed by value, so a copy of i from main is created{++i; //this increments the local copy of i}void bar(int &i) //this is passed by reference, so it is essentialy the same variable as in main{++i; //this increments i, which is an alias for the i in main, so that variable changes}

Share on other sites
Quote:
 Original post by NUCLEAR RABBITOhhhhh ok, so because there is no declaration of points within the bonus function, it looks up one scope and find points is = to 100, it uses that value, correct?

The bonus function declares points at the start. The bonus 'points' is completely seperate from main. No relation. At all. It just has the same value, but it is a different Variable.

If you used the reference, points is the same varialbe. Just like you never changed scopes.

Edit: Ha, beaten to the punch

Share on other sites
Quote:
 Original post by NUCLEAR RABBITHello,As you may know, Im working with functions, and I want to make something clear.In the following code, is the function bonus(), it seems that it takes the value of points from main(), but dont you need a &'s when getting the value from another function?

One thing that may be confusing use is the use of the same variable name "points" in two places, but that doesn't mean they necessarily contain the same data. You could have named the variables like this:

#include <iostream>using namespace std;int bonus(int input_points_value);int main(){    int main_points = 100;    cout << "Your points: " << bonus(main_points);    system("pause");    return 0;}inline int bonus(int input_points_value){    return (input_points_value-10);}

And here's with references the others mentioned:

#include <iostream>using namespace std;int bonus(int input_points_value);void half_bonus(int &reference_to_points);int main(){    int main_points = 100;    cout << "Your points: " << bonus(main_points);    cout << "Main points unchanged: " << main_points;    cout << "Half points: " << half_bonus(main_points);    cout << "Main points changed: " << main_points;    system("pause");    return 0;}inline int bonus(int input_points_value){    return (input_points_value-10);}inline void half_bonus(int &reference_to_points){    reference_to_points = reference_to_points/2;    return;}

I didn't compile so forgive minor typos.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627677
• Total Posts
2978589

• 11
• 12
• 10
• 12
• 22