"functions"?

my teacher is asking us to:

"ask for the radius of the circle, send this radius to a function called circleArea(). circleArea() should calculate and return the area of the circle to main. main () should then display the radius of the circle and its area."

I don't really know how to do that. The way I would write that program would be:

double radius, areaCircle;
cout << "What is the radius of the circle?" <<endl;
cin << radius;
areaCircle = pi * pow (radius,2);
cout << "The radius of the circle is "<<radius<<". And the area of the circle is "<<areaCircle<<". Thanks."<<endl;



What am I supposed to do? I don't really know what functions and parameters are? They were on the last assignment but I didn't start till late, didn't finish. QQ (I'm doing this one early!!)
Last edited on
Hey, I am currently a student at UAT, but I think I can help you a bit. Your code is good, all you need to do is make the line that calculates the area into a function.
I'll give you an idea of how functions work:
First: functions are just separate blocks of code that the main block can call, passing arguments to them so they can do their job.
Second: arguments are any data items that the main program passes to the function so it can do its job.
Third: parameters are the data that the function needs to do its job.
Fourth: return types are mentioned before the names of functions, these are simply the type of data that the function will return to the main block after doing its job. Return them using the return command within the function code.

Function template:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
returntype functionName(parameters)
{
    //code to perform desired action goes here
    // don't forget to return the value, if there is data that should be returned
}

       //Then in the main() code, you just need to pass the variable to 
      //that function by putting it in the parentheses:

int main()
{
      functionName(argument);   //argument being the variable to pass
}



That is how basic function calls work and you should get the hang of it in no time, just keep at it.
Last edited on
The cin's correct use is:


cin >> radius;

look at the >>.
Last edited on
Topic archived. No new replies allowed.