C++ Call by reference

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 estout17  Icon User is offline

  • New D.I.C Head

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

C++ Call by reference

Posted 05 February 2015 - 12:19 PM

Below is my call by reference code. I am currently getting the errors below as well. I thought that the int already declared the variable.

Error 1 error C4700: uninitialized local variable 'baseball' used 12 1 Project4
Error 2 error C4700: uninitialized local variable 'basketball' used 13 1 Project4
Error 3 error C4700: uninitialized local variable 'vl' used 27 1 Project4
Error 4 error C4700: uninitialized local variable 'ar' used 27 1 Project4
Error 5 error C4700: uninitialized local variable 'vl2' used 28 1 Project4
Error 6 error C4700: uninitialized local variable 'ar2' used 28 1 Project4
Error 7 error C4700: uninitialized local variable 'totvol' used 29 1 Project4

#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;
	ar2 = 3 * basketball * basketball;
	vl = 4 * baseball * baseball * baseball;
	vl2 = 4 * basketball * basketball * basketball;
	totvol = vl2 / vl;
	return ;
}

int main()
{
	int baseball, basketball, ar, ar2, vl, vl2, 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.";

	return 0;
}

This post has been edited by modi123_1: 05 February 2015 - 12:20 PM
Reason for edit:: Please use the 'code' button in the editor to apply code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Call by reference

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2600
  • View blog
  • Posts: 4,153
  • Joined: 21-June 11

Re: C++ Call by reference

Posted 05 February 2015 - 12:42 PM

I don't see why you'd get any errors in the sphere function, as you're only using your parameters, which you'd never have to initialize yourself (after all they're supposed to be supplied by the caller). Are you sure you posted the exact code you got those errors for (since the line numbers in the error messages don't match, probably not)? Also you never actually call your sphere function.

The errors in the main function are because you legitimately never initialize the variables in question.
Was This Post Helpful? 1
  • +
  • -

#3 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: C++ Call by reference

Posted 05 February 2015 - 12:45 PM

I tried compiling your code, it compiles fine but has warnings:

Quote

||=== forum, Debug ===|
/home/anarion1191/Projects/C++/forum/main.cpp||In function ‘int main()’:|
/home/anarion1191/Projects/C++/forum/main.cpp|22|warning: ‘ar’ may be used uninitialized in this function [-Wuninitialized]|
/home/anarion1191/Projects/C++/forum/main.cpp|22|warning: ‘vl’ may be used uninitialized in this function [-Wuninitialized]|
/home/anarion1191/Projects/C++/forum/main.cpp|23|warning: ‘ar2’ may be used uninitialized in this function [-Wuninitialized]|
/home/anarion1191/Projects/C++/forum/main.cpp|23|warning: ‘vl2’ may be used uninitialized in this function [-Wuninitialized]|
/home/anarion1191/Projects/C++/forum/main.cpp|24|warning: ‘totvol’ may be used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 errors, 5 warnings ===|


Are you sure you are getting errors with the code you wrote here?
Was This Post Helpful? 0
  • +
  • -

#4 estout17  Icon User is offline

  • New D.I.C Head

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

Re: C++ Call by reference

Posted 05 February 2015 - 02:45 PM

I tried to compile it again and it will not compile. These are the correct errors with it.
What do you mean I do not call in the sphere function? This is my first time trying a call-by-reference so I am not exactly sure how it is done.
Was This Post Helpful? 0
  • +
  • -

#5 estout17  Icon User is offline

  • New D.I.C Head

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

Re: C++ Call by reference

Posted 05 February 2015 - 03:30 PM

I brought in the void sphere into the code. Is this the call by reference?

I am still getting these errors:

Error 1 error C2065: 'baseball' : undeclared identifier 12 1 Project4
Error 2 error C2065: 'basketball' : undeclared identifier 14 1 Project4
Error 3 error C2065: 'ar' : undeclared identifier 15 1 Project4
Error 4 error C2065: 'vl' : undeclared identifier 15 1 Project4
Error 5 error C2065: 'ar2' : undeclared identifier 16 1 Project4
Error 6 error C2065: 'vl2' : undeclared identifier 16 1 Project4
Error 7 error C2065: 'totvol' : undeclared identifier 17 1 Project4

#include <iostream>
using namespace std;

int main()
{
	void sphere(int ar, int ar2, int vl, int vl2, int totvol, int baseball, int basketball);
	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;
}
void sphere(int& ar, int& ar2, int& vl, int& vl2, int& baseball, int& basketball, int& totvol)
{
	ar = 3 * baseball * baseball;
	ar2 = 3 * basketball * basketball;
	vl = 4 * baseball * baseball * baseball;
	vl2 = 4 * basketball * basketball * basketball;
	totvol = vl2 / vl;
	return;
}


This post has been edited by Skydiver: 05 February 2015 - 03:32 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,228
  • Joined: 05-May 12

Re: C++ Call by reference

Posted 05 February 2015 - 03:35 PM

Please use code tags. It makes it much easier to read your code.

By any chance, do you have about 5 blank lines at the top of your program? Or 5 comment lines? The line numbers in the error messages you are posting do no line up with the lines of code you've posted, but they seem to be consistently 5 lines off.
Was This Post Helpful? 0
  • +
  • -

#7 estout17  Icon User is offline

  • New D.I.C Head

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

Re: C++ Call by reference

Posted 05 February 2015 - 03:43 PM

I took out the comment on the top and added in the notes.
#include <iostream>
using namespace std;

int main()
{
	void sphere(int ar, int ar2, int vl, int vl2, int totvol, int baseball, int basketball);
	cout << "Radius of baseball (inches): ";
	cin >> baseball; //user inputs radius of baseball
	cout << "Radius of basketball: ";
	cin >> basketball; //user inputs radius of 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;
}
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;
}

This post has been edited by jimblumberg: 05 February 2015 - 03:46 PM
Reason for edit:: Added missing code tags please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 5465
  • View blog
  • Posts: 17,015
  • Joined: 25-December 09

Re: C++ Call by reference

Posted 05 February 2015 - 03:46 PM

Please use code tags when posting code!!!!

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#9 estout17  Icon User is offline

  • New D.I.C Head

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

Re: C++ Call by reference

Posted 05 February 2015 - 03:52 PM

What are code tags? I thought those were the notes put in the code.

#include <iostream>
using namespace std;

int main()
{
	void sphere(int ar, int ar2, int vl, int vl2, int totvol, int baseball, int basketball);
	cout << "Radius of baseball (inches): ";
	cin >> baseball; //user inputs radius of baseball
	cout << "Radius of basketball: ";
	cin >> basketball; //user inputs radius of 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;
}
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;
}


Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: C++ Call by reference

Posted 05 February 2015 - 04:42 PM

Hi, if the function is written after you try and use it, you will need a function prototype.

Functions
Was This Post Helpful? 0
  • +
  • -

#11 estout17  Icon User is offline

  • New D.I.C Head

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

Re: C++ Call by reference

Posted 05 February 2015 - 08:27 PM

So it should be this? It is still giving me a number of errors.

Error 1 error C2065: 'baseball' : undeclared identifier 18 1 Project4
Error 2 error C2065: 'basketball' : undeclared identifier 20 1 Project4
Error 3 error C2065: 'ar' : undeclared identifier 21 1 Project4
Error 4 error C2065: 'vl' : undeclared identifier 21 1 Project4
Error 5 error C2065: 'ar2' : undeclared identifier 22 1 Project4
Error 6 error C2065: 'vl2' : undeclared identifier 22 1 Project4
Error 7 error C2065: 'totvol' : undeclared identifier 23 1 Project4


#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()
{
	void sphere (int ar, int ar2, int vl, int vl2, int totvol, int baseball, int basketball);
	cout << "Radius of baseball (inches): ";
	cin >> baseball; //user inputs radius of baseball
	cout << "Radius of basketball: ";
	cin >> basketball; //user inputs radius of 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
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon

Reputation: 5465
  • View blog
  • Posts: 17,015
  • Joined: 25-December 09

Re: C++ Call by reference

Posted 05 February 2015 - 09:54 PM

In main() where have you defined the variables you are trying to use in your print statements? What is the purpose of that function? Unless you actually call the function it won't do anything. By the way line 16 is not a function call it is a function prototype.

Jim
Was This Post Helpful? 1
  • +
  • -

#13 estout17  Icon 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 - 04:53 PM

in main the variables are defined as int in line 16. Am I not supposed to have a function prototype? How do I call it in then?
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,228
  • Joined: 05-May 12

Re: C++ Call by reference

Posted 06 February 2015 - 05:05 PM

Unfortunately, you are mistaken. Your line 16 is a function prototype. It does not declare or define any variables (except for the function name/pointer).
Was This Post Helpful? 0
  • +
  • -

#15 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: C++ Call by reference

Posted 06 February 2015 - 05:07 PM

No you dont need the prototype at line 16.

You have declared your function before main() so no need for a function prototype.

And you certainly dont put your function prototypes inside of main()

examples of how to declare functions


void myfunc()
{
// some code
}

int main()

{
  myfunc();
return 0;
}



or

void myfunc();


int main()

{
  myfunc();
return 0;
}

void myfunc()
{
// some code
}



The second code block needs a prototype the first doesn't.

Do you understand why ?

Regards

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

  • (2 Pages)
  • +
  • 1
  • 2