Hi, I'm not sure what this code does.

int total(int value1, int value2)
{
 int sum;
 sum = value1 + value2;
 return sum;

}

This method should take in 2 values and calculate the sum and return it. But I don't understand where it returns it to? In my main method I had:

string Uinput;
    int num1;
    int num2;
cout<<"Enter the 2 numbers you want to add"<<endl;
getline(cin,Uinput);
getline(cin,Uinput);
stringstream(Uinput)>>num1>>num2;
total(num1,num2);
//I want to print the sum but how do I get it from the total function????

I want to print the sum but how do I get it from the total function???? I don't understand

The value is returned to the location where the function call came from

total(num1,num2);

However, you didn't assign it to a variable. The return statement returns the value sum to the function call.

You can do it two ways, either set the returned value to a variable declared in the block of code where you used it, or print the value directly.

foo = total(num1,num2); //foo holds sum

or

cout << "The value is: " << total(num1, num2);

Either way should work.

commented: Thanks alot +0

As written, total() returns the value of sum, but that return value then gets discarded because you aren't doing anything with it. You have 2 options:

  1. store it to a variable, then output the variable
    int someInt = 0;
    someInt = total(num1, num2);
    cout << someInt;
  2. output the return directly
    cout << total(num1, num2);
commented: Thanks alot +0

hmm, Ok it is printing something, but it is not the sum. It prints a number like
-85887934. Should I be referencing the values I put into the method or something?

Ok I got it, It seems you cannot use stringstream for 2 separate numbers on the same statement. I just split the 2 inputs into 2 stringstreams. Thanks guys.

Wait a minute, why are you reading them as strings in the first place? Why not just read the integers directly? Is that a requirement for an assignment or something?

Wait a minute, why are you reading them as strings in the first place? Why not just read the integers directly? Is that a requirement for an assignment or something?

it means I have more control over what happens with the input of the numerical values because I am separating the the process of obtaining input from the user with the interpretation of that input. Therefore, It is actually the preferred method to get numerical values from the user in input intensive programs.

I see... But, the question is, do you understand why? Or is it just "because it's in my notes"?

I find it odd that you can make such a heady academic claim like that, but you don't understand how to use a return value.

I always thought reading as a string, then type casting to something else was always a hassle because of coding the error catching. In what way does it give more control?

I see... But, the question is, do you understand why? Or is it just "because it's in my notes"?

I find it odd that you can make such a heady academic claim like that, but you don't understand how to use a return value.

I basically only ever used void methods, or had print messages inside a method that needed a returned value. I have just never really attempted to find out about it because I seemed to be able to get away without it, but I decided I should learn about it. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.