### Trouble with Functions/Parameters

Hi guys, so I'm having a bit of trouble with this code. The purpose of this code is to try and calculate the sum of the integers 1-10 (inclusive). The answer SHOULD be 55, but I keep getting 0 and I'm not sure why/how I can fix the code so that it calculates it correctly. I'm new to C++ and have no clue on void functions/parameters. If anyone could explain it would
mean a lot. Thanks!

Edit: Btw, I did compile, link, run, etc. There are no compiling errors. It just says that "The sum is 0".

EDITx2:
I do know that the sum of the numbers aren't displaying correctly inside the main function and so I need to adjust the code so that main "calls" the function and then it should be able to display the sum correctly, right? I'm having trouble with that portion of it.
Oh and a few requirements I had in working on this is that I'm not supposed to use global variables or calculate anything inside the main function itself.

 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace std; void compute_sum(int); int main() { int sum = 0; int b = 10; compute_sum(b); cout << "The sum is " << sum << endl; return 0; } void compute_sum(int x) { int sum = 0; int num = 1; while (num <= x) { sum += num; num++; } }``````
Last edited on
The sum variable in the compute_sum() function is separate from the one in main(). Try returning the value instead.
Hmm, would you be able to explain by what you mean when you say "returning the value?" I'm a bit confused, sorry!
Read over this part of the tutorial:
http://cplusplus.com/doc/tutorial/functions/

It should cover about returning a value and probably about variable scope as well. Post back if you still have questions.
It's still not working. I didn't really understand the tutorial, but I tried returning the value like they did in the example except the value didn't change and I got an error saying:
"error: return-statement with a value, in function returning 'void' [-fpermissive]"

Would you be able to show it by any chance?

EDIT:
I do know that the sum of the numbers aren't displaying correctly inside the main function and so I need to adjust the code so that main "calls" the function and then it should be able to display the sum correctly, right?
Oh and a few requirements I had in working on this is that I'm not supposed to use global variables or calculate anything inside the main function itself.
Last edited on
If you want to return something, then you must say what do you want to return
 ``1234567891011`` ``````int compute_sum(int x) { int sum = 0; int num = 1; while (num <= x) { sum += num; num++; } return sum; }``````
Last edited on
I tried to do that, but I still get an output of "The sum is 0". I also tried to remove the '0' (Line 8) in:
 ``123`` ``````int main() { int sum = 0;``````

and then the result ended up being 60 which didn't make sense to me. I'm not sure why it's saying 60.
At line 11 you need to assign the result of compute_sum to sum.
Ne555 already gave you the changes to compute_sum.
 ``1234567891011121314151617181920212223242526`` ``````#include using namespace std; int compute_sum(int); int main() { int sum = 0; int b = 10; sum = compute_sum(b); cout << "The sum is " << sum << endl; return 0; } int compute_sum(int x) { int sum = 0; int num = 1; while (num <= x) { sum += num; num++; } return sum; }``````

Oh wow, I completely missed that. Thank you so much! Greatly Appreciated (:
Topic archived. No new replies allowed.