need help - how do references work in c++?
42 minutes ago, Pachuca said:~snip~
function1: They need to be referenced because you want to change the value of the passed parameters from within the function. A referenced parameter is passed as a reference of the original variable while a standard parameter is passed as a distinct copy of the variable, so the changes would be lost at the end of the function.
function3: The exact opposite of the argument above. You don't *want* to change any numbers inside the function so you pass copies. The value of the function is it's return which is getting assigned to a variable in your main. It's a double because it returns a double. The other functions are void because they return nothing.
funcOne: Because, in the last line of this function, you change the value of x, and you want that change passed up to the higher scope so you use a reference, but you don't want to change anything about y with this function so you pass it as a copy.
nextChar: Because it increments z and you want that change to be passed up.
P.S. something's screwy in the code... z isn't defined above where it's used in the main loop, amount in function 3 isn't defined and it always returns 0, and function 3 is written as a void function even though it should be a double function. Are these typos?
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now