reference parameters with 2 values

i need to write a function named compute that is based on a circle’s radius, compute should compute the area and circumference and saves those values to two reference parameters. With that i need my main function that asks the user to enter the radius of a circle then reads the radius and calls the compute function to calculate the area and the circumference. And then obviously outputs the area and circumference.

how am i suppose to do this? i really don't understand reference parameters
The wording is strange here, but from what I can tell they're wanting:

void compute(int radius, double &area, double &circ);

And just store the answer in the two references.
i really don't understand reference parameters

This page should help you to understand what it means and how it works
http://www.cplusplus.com/doc/tutorial/functions2/
thank you! you were both helpful. This is what i have and i think i am close but when i try to compile it says
In function ‘int compute(double)’:
error: ‘radius’ was not declared in this scope

This is my function
#include <iostream>
using namespace std;

int compute(double n)
{
int a;
a = 3.14 * radius * radius;
int c;
c = 2 * 3.14 * radius;
}

int main ( )
{
int radius;
cout << " Enter radius:";
cin >> radius;
cout << compute (radius);
return 0;
}
Use the function header recommended by ResidentBiscuit:
void compute(int radius, double &area, double &circ)
instead of what you currently have:
int compute(double n)

That will solve the compiler error you are getting.
But more importantly, it will enable the program to achieve the required results.

By the way, the cout statement cout << compute (radius); will not work with the new improved function. Instead you need to first call the function (with the radius as input), and afterwards, output the values.


If you're still confused, look again at function "duplicate" on the tutorial page you studied previously.
Last edited on
Well, for once thing you declare your compute() function to return type int, but you have no return statement. You can fix this one of two ways. One would be to use the declaration that Cherv and RB have recommended, and have it return nothing (void) but pass area and circ as references, that way when you modify them in the function you are actually modifying the variables you passed to the function as well. Passing by reference is more efficient if you plan to modify data elements which is why it's preferred for classes and structures.

If you don't want to do it that way you will have to add a return statement to your function. Here's the catch though: Functions can only return 1 thing. So you'll have to write 2 separate functions to calculate and return area and circumference. This is another reason why passing by reference is more efficient.

Now, as for why you are getting that "not declared in this scope" error is because you declared the variable radius in the main() function. Once execution leaves main() (ie, is passed to compute() by the function call) any variables in main() that aren't passed by reference or pointer to the function become, in effect, invisible. If you want to use radius inside compute() you either have to declare it globally (outside of main(), IE before it.) Or declare double n as double radius instead (which is what I think you were actually meaning to do. Also not that ints A and C are pointless here since you don't return anything. Sure, they'll hold the area and circumference that you compute, but once you leave that function they are gone and so is the data they held.

Honestly, I'd recommend the fix that Cherv and RB are suggesting. References might be beyond what you've learned atm though, but if you don't use them you'll have to write a separate compute() function for both area and circumference to return the computed values back to variables in main.
Topic archived. No new replies allowed.