I am trying to solve this question (homework) that says the following :

Write a program that calls a function computeSphere that computes the volume and the surface area of a sphere with given radius. The function should not perform any I/O operations.

the main problem is how can I let my function (computeSphere) return 2 variables to main function ??

this is my code :

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int r;
int computeSphere(int r)
{
		double a,v;
	a = 4*3.14* pow(r,2);
	v = (4/3) * 3.14 * pow(r,3);
	
return a,v;
}

int main()
{

	cout << "Enter the radius: ";
	cin >> r;


		cout << fixed << setprecision(2);
	
	computeSphere(r);

	cout << "The area of a sphere of radius " << r << " is " << a << " and its ";
  cout << "volume is ";
  cout << v;
  cout << endl;

return 0;
}

It gives me 2 errors while compiling :

error C2065: 'a' : undeclared identifier
error C2065: 'v' : undeclared identifier

any help is appreciated :).

Recommended Answers

All 8 Replies

Call computeSphere(r, &a, &v); & will pass the address of the variable and the value can be changed in the function.

Thanks ,
That solved the problem, It turned out I needed to use pointers to solve the question :)
as there is no way a function could return 2 variables

That solved the problem, It turned out I needed to use pointers to solve the question

Preferably references. Pointers are a waste of processor when you can use references instead.

Preferably references. Pointers are a waste of processor when you can use references instead.

Serious question: What's the difference...

I though reference was just the C++ term for pointer, like folder is the Winblows term for directory.

And if they are different, how are pointers a waste of processor?

A pointer is an object. It's usually the same size an an int, it stores a single numerical value (i.e. the memory address to which it "points"), and has the special abilities of "being dereferenced" to get the object it points at, and pointer arithmetic when adding or subtracting to it.

int a;
int* b;

In this code, I made two objects. One, an int, named a, which probably takes up 4 bytes; and another object, a pointer (to an int), named b, which probably takes up another 4 bytes (maybe 8, depends on your system etc).


A reference is essentially another name for the exact same object;

int a;
int& b;

In this code, I created only one int. Just one. b is another name for the int known as a. Thus, the reference does not take up any more memory; it's not an object. It's just another name for some object (note that how a compiler makes references work is up to the compiler - whilst smart compilers don't waste time and memory, it's not mandated, and behind the scenes I think there's nothing stopping them being bloody silly and doing something expensive; I haven't read the C++ 11 definition yet, so if that has now changed, feel free to tell me!)

When I pass a pointer into a function, I am passing-by-value. The pointer is copied and passed into the function. This takes up time, to make the copy, and memory, to hold onto it. If all I want to do is make sure that the function can affect the original object in the calling code, it seems a waste to make pointers and copy them around, when I could pass in a reference.

For those just joining in, here's passing-by-reference in play.

void makeAnIntBigger(int& input)
{
  input++;
}

int a = 7;
makeAnIntBigger(a);
// now, a has the value 8, and I didn't have to make any pointers, which costs time and memory

When I pass-by-reference, no extra variables are made and copied; the function gets the actual object.

Here's more reference fun:

#include <iostream>

int main()
{
int a = 7;
int& b = a;
int& c = b;

b++;
c++;

 std::cout << a << " " << b << " " << c;
 return 0;
}

Sounds to me like a reference is a pointer with hidden code to make it look like a variable. The function has to get either
1) the value of the variable, which is local or
2) the address of the variable, which is a reference.

No need to continue this here. I'll look up the inner workings -- unless Narue chimes in. I like the way she explains things...

Sounds to me like a reference is a pointer with hidden code to make it look like a variable.

Since a pointer takes up memory, and can be dereferenced, and can have pointer arithmetic applied to it, and a reference does none of those things, they're clearly not at all the same and to think of a reference as just a pointer that's permanently dereferenced is missing the point.

The function has to get
1) the value of the variable, which is local or
2) the address of the variable, which is a reference.

A reference is not an address. A reference is the object. With references, the function can now also get
3) The actual object. Not a copy of it, not a pointer whose value is the address of it; just the actual, original object.

Really, references are not just some kind of permanently dereferenced pointer. They're worth reading up on. In the nicest possible way, to think of a reference as a kind of permanently dereferenced pointer is (a very common way) to miss the point.

But yes, this thread must die :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.