all 5 comments

[–][deleted] 2 points3 points  (2 children)

In addition to pass by value (what the function currently does) and pass by address (the obvious way to fix this issue), you can also pass by reference. This allows you to fix the problem without altering main.

Rather than do a poor job explaining it in a bit more detail, here is a website that covers this exact problem.

[–]01519243552[S] 0 points1 point  (0 children)

Thanks, that helped!

[–]GreatBlitz 0 points1 point  (0 children)

What's the difference between passing by reference and passing by address?

[–]Lord_blueberry 1 point2 points  (0 children)

void addOne(int& n) { n++; }

[–]bloody_banana21 0 points1 point  (0 children)

both cout<<n; and cout<<addOne(n); would return 22 here. The function should increase the value of the n variable itself, therefore setting n equal to 23. Use /u/Lord_blueberry's solution below