Jump to content

need help - how do references work in c++?

Go to solution Solved by Sniperfox47,
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?

I'm trying to figure out how user declared functions operate with references. This is one of my homework's and I'm a bit confused. I have to make a series of functions that have to be called in main. for example this code is wrong and I need to understand why. I know how to write it the write way because someone showed it to me, but I can't figure out why this is wrong. 

 

void function1(int x, int y, char a);

void function2(double num1, double num2); 

void function3(double num1, double num2);

void function4(double num1, double num2, double num3);
void funcOne(int x, int y);
void nextChar(char z);

 

int main(){
int x, y;

char a;

double num1, num2, num3;

 

function1(x, y, a);

function2(num1, num2);

num3 = function3(num1, num2);

function4(num1, num2, num3);
    cout << "The value of x is currently: " << x << endl;
    funcOne(x, y);
    cout << "The value of x is now: " << x << endl;
    nextChar(z);
    cout << "The value of z is now: " << z << endl;

 

return 0;
}

 

void function1(int x, int y, char a){

x = 1;

y = 0;

a = 'a';

}

 

voind function2(double num1, double num2){

cout << "Please enter num1: "; 

cin >> num1;

cout << "Please enter num2: ";

cin >> num2;

}

 

void function3(double num1, double num2){

double total = 0;

    if (num2 > 4) {
        amount += ((num1 - 4) * num2) * 2;
        amount += 4 * num2;
        return total;
    } 
    else {
        amount = num1 * num2;
        return total;
    }

 

}

 

void function4(double num1, double num2, double num3) {
    cout << "For num2 " << num2 << "  and num1 " << num1 << " total ";
    cout << fixed << showpoint << setprecision(2);
    cout << num3 << endl;
}

void funcOne(int x, int y) {
    int tempNum;
    cout << "Please enter a number: ";
    cin >> tempNum;

    x = (x * 2) + y - tempNum;
}

 

void nextChar(char z) {
    z++;
}

 

 

I'm confused why somethings need to be referenced and others don't. the correct way to write the code would be to write the functions like this:

 

void function1(int& x, int& y, char& a); //why do these have to be referenced?

void function2(double& num1, double& num2); 

double function3(double num1, double num2); //why does this not have to be referenced like the above two? Why does it have to be double and not void? 

void function4(double num1, double num2, double num3);
void funcOne(int& x, int y); //why is only x referenced here?
void nextChar(char& z); //why does this have to be referenced? 

 

 

 

Link to comment
https://linustechtips.com/topic/806559-need-help-how-do-references-work-in-c/
Share on other sites

Link to post
Share on other sites

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?

Link to post
Share on other sites

36 minutes ago, Sniperfox47 said:

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?

there are some mistakes, i tried to type it as best as I could. function 3 is double, I wasn't sure why until I saw the error message saying void can't convert to double. 

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×