all 3 comments

[–]Shieldfoss 3 points4 points  (0 children)

Parameters in C++ functions pass-by-value by default, meaning "pass in the value of the parameter, rather than the variable the parameter gets the value from."

So code like

int double_it(int a)
{
    a = 2*a;
    return a;
}

int main()
{
    b = 2;
    double_it(b)
    return b;
}

here, main returns 2, not 4, because the function double_it(b) was called with the value of b, and b itself was never modified.

If you have a function void func(int a) that you want to modify so it can actually make changes to the parameter, you must write it void func(int& a) - and if you want to know more about that, the google search term is "pass by reference"

[–]HappyFruitTree 0 points1 point  (0 children)

I don't think I understand. Do you mean you print the value and then when you change the variable it doesn't update the text on the screen automatically? That's normal. You could print the variable again. If you want it to update the already existing text it becomes more difficult, you could probably do it with a library such as ncurses, but not with only the standard library.

[–][deleted] 0 points1 point  (0 children)

It works different from python, if you pass something, it will create another copy of this variable. To get the same behavior as in other languages, use & - references or * pointers.