User defined function returns unexpected value

Nov 8, 2012 at 6:28pm
Hi, i'm new to programming and don't have much experience, but have decided to take a course at uni. We've been doing user defined functions and have got stuck on this one. Basically, the code compiles and runs, but it only returns a value of 1, no matter what i set the variables to. The problem is in the volcyl function. I've posted the code below:

#include <iostream>
#include <cmath>

using namespace std;


//Function prototypes:

double volcyl(double radius, double height); //returns the volume of a cylinder

int main()
{
double radius = 1; //sets value of radius
double height = 1; //sets value of height
cout << "r h V" << endl;
cout << radius << " " << height << " " << volcyl << endl; //cout radius, height and volume of cylinder

return 0;
}

//Define function "volcyl" - calculates the volume of a cylinder


double volcyl(double radius, double height)
{
const double PI = 3.14159265359;

return (PI*pow(radius,2)*height);
}

It's probably a simple thing i've got wrong but i've been trying to get it to work for the last hour with no success. I've tried re-organising the code to no avail, and have had a good route round on the internet, for a solution but can't find one.

Like i said it's probably a simple problem but i can't figure it out, so any help would be appreciated.

Thanks in advanced
Nov 8, 2012 at 6:40pm
The problem is that instead of calling the function you are trying to output its name that is impliciitly converted to bool value true.:)

Change

cout << radius << " " << height << " " << volcyl << endl; //cout radius, height and volume of cylinder

to

cout << radius << " " << height << " " << volcyl( radius, height ) << endl; //cout radius, height and volume of cylinder

Nov 9, 2012 at 12:53am
Thanks, i knew it would be something simple, but i just couldn't find it. Thanks for your help :)
Topic archived. No new replies allowed.