problem with a simple function

Hello!!
So I need a little help with this function I am trying to write.
It is very simple beginner stuff but I really am not seeing where I am going wrong.

all I want to do is to take a variable and feed it to a function that will execute a sum and give me an answer which should be 225 but this produces 1.
I really don't think I'm so far off but maybe I have been looking at it for too long.

#include <iostream>
using namespace std;
int SUM1 (int x, int s)
{
int n;
n = 5;
int r;
for(x = 1; x <= n; x++){
r = s + (x * x * x);
}
return (r);
}
int main()
{
int t;
t = SUM1 (5,0);
cout << SUM1 << endl;
system ("PAUSE");
return 0;
}

Thanks to all
According to your calculation, s is always 0. What is the calculation you're trying to perform?
I want s to start at 0. Doesn't
r = s + ( x * x * x );
inside the loop change that for me once s is calculated? the first s would be 1 and then 8. Do I need to take s out of the loop?
Well for one thing, you're storing the result of SUM1(5, 0) in t:

t = SUM1 (5,0);

But you're not printing t, you're printing SUM1:

cout << SUM1 << endl;

That's going to print out the address of the SUM1 function in memory. Secondly, I don't know what you're trying to calculate exactly, but I'm pretty sure your function is wrong. Why does it have a parameter called x when you overwrite its value with 1 before you use it for the first time?
You need to do:

r += s + (x * x * x);

Make sure you set r to 0 before using it.
Last edited on
RE: JellyFox
I did that with int t because I need to declare a variable in main which calls upon my function. Or should I say that is what I believe needs be done to get the function to work in main.
2ndly I am trying to define my variable n and pass it to a function that will produce a sum of the first n cubes.
I wrote the sum just as a non function program and now I want to make the program work with a function.

<<Taken from the C++ tutorial>>
// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
<< >>
I know it's not the same function but I thoughtfully tried to transpose one onto the other, mine needs a for loop to work.

RE: davedale
no luck.
Topic archived. No new replies allowed.