### Little help with functions c++?

Pages: 12
I'm trying to get this program to work but something is wrong, don't know what exactly. The formula - if you're wondering - for the volume of a sphere is
4/3 * 3.1416 * radius(cubed)

are my functions wrong? it says num2 isn't initialized to something. help.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` `````` #include using namespace std; const float PI = 3.1416; float SphereVol(float, float); float Cube(float); void main() { float radius, num2; cout << "Enter a radius: "; cin >> radius; cout << "Volume of a sphere with a radius " << radius << " is " << num2; } float SphereVol(float radius, float r) { float num2; num2 = ( 4 / 3 * PI * r ); return num2; } float Cube(float radius) { float r; r = ( radius * radius * radius ); return r; }``````

well um you never actually called your sphere volume function after getting the radius then you tried to output the volume. try putting your function on line 17 right now if you output num2 you're probably getting something like 644123123
Last edited on
That's beause you try to cout the value of num2 while it is "dirty"(not initialized/set to anything). You must give it a value before trying to cout it.

Aceix.
Last edited on
main() should never ever be of type void. Ever.
i'm trying to call SphereVol so it displays on the output, but something is wrong. do I need to modify the function (SphereVol) or main?
num2 = sphereVol(radius); not sure what the 'R' is there for
THat is how you set a variable equal to the return of a function with a paramater of something

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

These will help
Last edited on
yep i did what it says.

First i made function prototypes,
then I calculated the cube in Cube function
then I calculated SphereVol, calling Cube() in SphereVol.

what I want to do is to call SphereVol to main so it will displays the total. but apparently there's a mistake when i call the function
How do you call it?...I mean post your new code.

Aceix.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include using namespace std; const float PI = 3.1416; // Do (1) - constants float SphereVol(float, float); // Do (2) - function prototypes float Cube(float); void main() { float r, radius; cout << "Enter a radius: "; cin >> radius; cout << "Volume of a sphere with a radius " << radius << " is " << r; // Do (3) - print output and call SphereVol() } float SphereVol(float radius, float num) { float r; r = ( 4 / 3 * PI * num ); // Do (4) - calculate SphereVol, calling Cube() return r; } float Cube(float radius) { float num; num = ( radius * radius * radius ); // Do (5)- calculate the cube return num; } ``````
you still have NOT called it in the main function. calling a function is this
 ``1234`` ``````int main() { secondFunction(); }``````
it's still doesn't work. what could be the problem?
You should remove the radius parameter from SetSphere function, since it is not being used, and do this:
`r=SphereVol(radius);`
in the main function, just before cout-ing r.

Aceix.
You can read: http://cplusplus.com/doc/tutorial/functions/ for more information on functions.

Aceix.
yep it worked now, but it says that the formula is not right even though I'm pretty sure i did what it say :|
Do it this way:
 ``12345`` ``````{ float r, radius; cout << "Enter a radius: "; cin >> radius; r = SphereVol(radius); //put it here!!! cout << "Volume of a sphere with a radius " << radius << " is " << r; // Do (3) - print output and call SphereVol() } ``````

You should put it after cin in a value to radius, as I've done above.

Aceix.
Last edited on
That would be because your sphere function doesn't call the cube function when it should.
@Ispil

how can I call the cube function inside the sphere function?
Replace num with Cube(num).
nass before any of us CAN help you need to read those two pages I linked you. That is a pretty basic thing and I already told you how a couple of times.
You are not calling the functions you need to call them by something like
`function(parameter);`
 http://www.cplusplus.com/doc/tutorial/functions/ http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
@gilblit

I've been reading them and I couldn't get what's wrong. I'm an absolute beginner in c++ and sometimes i run into things that I don't understand well

did that but it's still doesn't show the correct answer

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` `````` #include using namespace std; const float PI = 3.1416; float SphereVol(float num); float Cube(float radius); void main() { float r, radius; cout << "Enter a radius: "; cin >> radius; r = SphereVol(radius); cout << "Volume of a sphere with a radius " << radius << " is " << r; } float SphereVol(float num) { float r; r = ( ( 4 / 3 ) * PI * Cube(num) ); return r; } float Cube(float radius) { float num; num = ( radius * radius * radius ); return num; } ``````

Edit:

I made some changes to my code and it's still not working (tried to call it)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include using namespace std; const float PI = 3.1416; // Do (1) - constants float SphereVol(float num); // Do (2) - function prototypes float Cube(float radius); void main() { float r, radius; cout << "Enter a radius: "; cin >> radius; r = SphereVol(radius); cout << "Volume of a sphere with a radius " << radius << " is " << r; // Do (3) - print output and call SphereVol() } float SphereVol(float num) { float r, radius; Cube(radius); r = ( ( 4 / 3 ) * PI * Cube(num) ); // Do (4) - calculate SphereVol, calling Cube() return r; } float Cube(float radius) { float num; num = ( radius * radius * radius ); // Do (5)- calculate the cube return num; } ``````

Last edited on
Pages: 12