Return Statement

I'm now at the Return Statement, but the book fails at explaining it
The example they give is:
1
2
3
4
5
6
7
double CubeOf(double x)
{
return(x*x*x)
}

for (Row=1; Row<=4; Row++)
cout<< Row<<" "<<CubeOf(Row)<<endl;

Just looking at this code I can tell it wont compile.
Could someone please explain to me how to use the return statment, or link me to a site that does?
In the function itself, there's a semicolon missing.

return x*x*x;

Are you asking how returning values works as a general concept?
Last edited on

In the function itself, there's a semicolon missing.
 
return x*x*x;



I noticed That as well, But the book has no semicolon so i assumed it was a typo in the book.

And yes I am asking how to return a value.
Well, you're doing it right now. Oh, and don't worry so much about typo's, it won't be the last one.
As for the "how to"... that's exactly how it works, there isn't anything else to it.
You have the return keyword, followed by an arbitrary expression. The function call evaluates to the returned value, so cubeOf(3.0) is equal to 27.0.

1
2
3
4
double cubeOf(double x)
{
  return x*x*x;
}
Topic archived. No new replies allowed.