C++ Call by reference

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1156 Views - Last Post: 06 February 2015 - 05:41 PM Rate Topic: -----

#16 estout17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-February 15

Re: C++ Call by reference

Posted 06 February 2015 - 05:13 PM

I understand exactly why. The function does not need a prototype if it is before. Does this look more like it?
#include <iostream>
using namespace std;

void sphere(int& ar, int& ar2, int& vl, int& vl2, int& baseball, int& basketball, int& totvol)
{
	ar = 3 * baseball * baseball; //equals the area of the baseball
	ar2 = 3 * basketball * basketball; //equals area of the basketball
	vl = 4 * baseball * baseball * baseball;  //equals area of the baseball
	vl2 = 4 * basketball * basketball * basketball; //equals area of the basketball
	totvol = vl2 / vl; //equals the total volume
	return;
}

int main()
{
	
	sphere(int& ar, int& ar2, int& vl, int& vl2, int& baseball, int& basketball, int& totvol);
	cout << "Radius of baseball (inches): ";
	cin >> baseball;
	cout << "Radius of basketball: ";
	cin >> basketball;
	cout << "Elise has a baseball that has a cross sectional area of " << ar << " square inches and a volume of " << vl << "cubic inches.";
	cout << "Elise has a basketball that has a cross sectional area of " << ar2 << " square inches and a volume of " << vl2 << " cubic inches.";
	cout << "The basketball is " << totvol << " times the volume of the baseball.";

	system ("pause");

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#17 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1550
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C++ Call by reference

Posted 06 February 2015 - 05:16 PM

Have you actually tried running it ?

But anyway the answer is No.
Was This Post Helpful? 0
  • +
  • -

#18 estout17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-February 15

Re: C++ Call by reference

Posted 06 February 2015 - 05:25 PM

Yes and it is giving me 10 errors that I am trying to figure out right now.

Error 1 error C2144: syntax error : 'int' should be preceded by ')' 17 1 Project4
Error 2 error C2660: 'sphere' : function does not take 0 arguments 17 1 Project4
Error 3 error C2059: syntax error : ')' 17 1 Project4
Error 4 error C2065: 'baseball' : undeclared identifier 19 1 Project4
Error 5 error C2065: 'basketball' : undeclared identifier 21 1 Project4
Error 6 error C2065: 'ar' : undeclared identifier 22 1 Project4
Error 7 error C2065: 'vl' : undeclared identifier 22 1 Project4
Error 8 error C2065: 'ar2' : undeclared identifier 23 1 Project4
Error 9 error C2065: 'vl2' : undeclared identifier 23 1 Project4
Error 10 error C2065: 'totvol' : undeclared identifier 24 1 Project4
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6504
  • View blog
  • Posts: 22,278
  • Joined: 05-May 12

Re: C++ Call by reference

Posted 06 February 2015 - 05:29 PM

It looks like you are just throwing code at the monitor to see what sticks. Please take 20-30 minutes to go back to post #10 and follow the functions tutorial there, or look at the function tutorial links in jimblumberg's signature (see post #8). They will save you a lot time instead of your approach of hoping to luck into the answer.
Was This Post Helpful? 0
  • +
  • -

#20 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1550
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: C++ Call by reference

Posted 06 February 2015 - 05:41 PM

I would also recommend you buy a better book than the one you have.

If you have one.

Snoopy.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2