I have tried various ways of returning a variable inside a loop in the function called tSumGen(generates triangular numbers) but it never works when I call it in main. The code is very simple but I cant find a way to make it work, I'd like to know if there is a way to return the value of the variable inside the last moment in the loop so I can then do some extra with that value in main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int tNumGen(int p)
{
int s=0;
for(int i=p; i>0; i--)
{
s=s+i;
}
return s;
}
int main()
{
tNumGen(7);
}
The value that should be getting returned to main is 28 but it doesnt show up.