function w/reference parameters

I am trying to write a function with four input parameters which displays an output but also accepts an input from main. I cannot figure out how to input variables that are referenced in the function. I want to be able to input a value for both radius and height and also get the calculations (volume and surface area) from the arithmetic to show up there as well. I realize I do not have all my math expressions yet. I sure hope that makes sense.

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

const double pi = 3.14159;
double rad;
double hei;
const double sla = 13.4164;
double area;
double r;

void output()
{

cout << setw(10) << "Results:" << endl;
cout << setw(68) << "For a cone with a radius of "" inches and a height of "" inches" << endl << endl;
cout << setw(48) << "Cone Volume: 452.38896 cubic inches" << endl;
cout << setw(49) << "Cone Surface Area: 365.99021 square inches" << endl << endl;

}

int main()
{
double pi;
double rad;
double hei;
double sla;
double area;
double r;

cout << "Program to calculate the volume and surface area of a cone" << endl << endl;
cout << "Enter cone radius (in inches):";
cin >> rad;
cout << endl;
cout << "Enter cone height (in inches):";
cin >> hei;
cout << endl << endl;

r = pow(rad, 2);
area = (pi * r) + (pi * (rad * sla));
output();



system("PAUSE");

}
Last edited on
Each function can only "return" one value. If you want to calculate more than one value in a single function, you need to give the function the "address" of that value so it can manipulate the label directly.

For a cylinder, the code could look something like this:
1
2
3
4
5
6
double Calc_cylinder(double r, double h, double *SA)
{
	const double pi = 3.14159;
	*SA = 2*pi*r*r + 2*pi*r*h;
	return pi*r*r*h;
}


This function returns the volume and will also set the surface area. To use it, you can put it into your main like so:
volume = Calc_cylinder(rad , hei , &area);

Note the "&" attached to the area. This means that you are passing the address only. In the function it has a "*" appended to it. This reads as "The value at this address".

Also note that the variable names do not need to be the same when you call the function, versus when you define the function. Only the values of those variables are passed into the function and so the name is irrelevant (with the exception of &area where the address is passed instead of the value).

You are welcome to put couts in the function itself, but I recommend doing that in your main so that you can use the function over and over.

Oh. and can you please use [code] and [ / code] tags next time? It makes it easier to read.
Last edited on
That helps me a lot, thank you. I should have been more detailed in my post but after 20 hours of study I was a bit off. My situation is this:
I am writing a program to calculate and output the area and volume of a cone. I have to write and call a separate, user-defined function to output the results. I should have four input parameters which are the radius, height, volume, and area. No calculations should be performed from within the function. What I don't get is how I write it so I can input the radius and height variables into "main" and have these change in the output function as well. I understand the void function w/no parameters but cannot get beyond that. I'm pretty sure I am over complicating things. I have read so much about functions that now I'm not sure which type to use.
you need an output function that takes in parameters and just prints them out? You probably need something like the following.

1
2
3
4
5
6
7
void output(double radius, double height, double volume, double area)
{
    cout << setw(10) << "Results:" << endl;
    cout << setw(68) << "For a cone with a radius of " << radius <<  " inches and a height of "<< height << " inches" << endl << endl;
    cout << setw(48) << "Cone Volume: " << volume << " cubic inches" << endl;
    cout << setw(49) << "Cone Surface Area: " << area << " square inches" << endl << endl;
}


Then you call it inside main and pass in the values into the function.
Last edited on
THANK YOU!! Your post led me to the correct section in my text which helped me figure out what I have to do. I was so close for the last 48 hours but I was not correctly passing the value of the variable to the formal parameter. Nor was I correctly transferring control to the function. Aside from writing some expressions, here's what I have so far.

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

const double pi = 3.14159;
double rad;
double hei;
const double sla = 13.4164;
double area;
double r;

void output(double radius, double height, double volume, double area)
{
double rad = 0;
double hei = 0;
double vol = 0;
double are = 0;
cout << setw(8) << "Results:" << endl;
cout << setw(27) << "For a cone with a radius of " << radius << " inches and a height of "<< height << " inches" << endl << endl;
cout << setw(24) << "Cone Volume: " << volume << " cubic inches" << endl;
cout << setw(19) << "Cone Surface Area: " << area << " square inches" << endl << endl;
}

int main()
{
double pi;
double radius;
double height;
double sla;
double are;
double r;
double vol;
cout << "Program to calculate the volume and surface area of a cone" << endl << endl;
cout << "Enter cone radius (in inches):";
cin >> rad;
cout << endl;
cout << "Enter cone height (in inches):";
cin >> hei;
cout << endl << endl;

r = pow(rad, 2);
area = (pi * r) + (pi * (rad * sla));
output(rad, hei, vol, are);



system("PAUSE");

}
A few comments:

You do not need to declare the following in your output function. Remove it, it will make it easier to read.
1
2
3
4
double rad = 0;
double hei = 0;
double vol = 0;
double are = 0;


You do not use the following in your main(). Remove them
1
2
double radius;
double height;


double sla is declared and used, but you never set it! This will return garbage. I assume that in your area calculation, you meant to use hei. Remove double sla and all references to it from the code or set it.

You use cin >> hei but you never declare hei. perhaps this was meant to be sla. Set something meaningful and remove hei or declare hei and use it.

you set the variable "area" but you do not declare it or use it as an argument in output(). I assume this is a typo and that you mean to use "are".

You declare and use "pi", but it is not set to anything! Make sure you initialize its value or it will get some junk that was in memory prior to you creating this variable.

I know sla and pi are set in the global scope (not in any functions), but I believe you are overwriting that when you declare them locally (inside a function). Remove all global definitions. It's generally a good idea.

you set r = pow(rad,2) then use r only once. That's unnessesary. From here I get finiky. Remove that line and replace the area calculation with the one below:
are = (pi * rad * rad) + (pi * rad * sla);
much easier!

Finally: Please use code tags! That helps us to read your code on this site.
Phew, that was quite a bit.
Last edited on
Thanks, i appreciate your help, now that I know what code tags are I'll use them. I also appreciate your detailed analysis as I have only been studying it for two weeks. It seems that when I hit a wall, just a little help goes a long way in answering many other questions.
Last edited on
Topic archived. No new replies allowed.