Noob needs help with foo and other things

So I'm just picking up C++ in college and I'm really enjoying it. However my teacher is a backup who has never taught C++ before and only codes in C. >Had to rant.< But anyways, my assignment is to have "int foo" above my "int main" to create a simple addition problem that pulls over into main. My homework requires the output to be in main but the actual equations and work to go on in foo.

I have two issues, the first is when I run the program without debugging it says that foo needs to return with a value. However if i return0; then it will delete everything in that code correct? How do I get around that? The second problem, is once I go down to int main() it states that variable A,B,C, and D are undeclared. How to I bring down the variables without making them a universal variable?

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  // Name: Jacob Chesley
// Date: 1/28/15
// Project: Function

#include <iostream>

using std::endl;
using std::cout;
using std::cin;

int A,
B,
C,
D;

int foo()
{
	cout << "Please enter three numbers: " << endl;
	cin >> A;
	cin >> B;
	cin >> C;
	cout << "The sum of these three numbers is: ";
	D = A + B + C;
	cout << D << "\n" << endl;
}
int main()
{
	cout << "You entered: " << A << ", " << B << ", and " << C << endl;
	cout << "Your sum of these numbers is: " << D << "\n" << endl;
	return 0;
}
Last edited on
Thank you so much for this. I've been working on this for hours and my teacher just never went over half this stuff.

My only question is, what is a pass by reference? I know that it works because I ran it. But what does it actually tell the computer to do and is it only used in this one respect?
pass by value and pass by reference.

pass by value creates a copy inside the function scope and gets destroyed after the function is finished.

pass by reference you just modify the value of an existing variable that was created outside the function.

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
Last edited on
Hi,

Have a look in the tutorials, reference , articles etc at the top left of this page :+)

Hope all goes well !
So I just want to confirm. When I pass by a reference, it becomes a constant, so long as I recall it using foo(A, B, C);. If I were to go to another declaration I could use A, B, and C again so long as I dont recall it from foo correct?
try taking out the & before A , B and C in the function declaration and run the program and you will see the difference.
Last edited on
Thank you again for all your help. This will be really useful in the future.
Hi,

Passing by reference is pretty much essential if the object is not a basic type, because it has potential to be large, thereby making it inefficient to copy the whole thing. So we pass by reference or by pointer, so only the memory address of the object is copied.

So if you have a std::vector, or std::string or any of the other std:: containers, or anything at all which is large, pass by reference.

References have the advantage over pointers, in that a reference must refer to a variable, whereas a pointer can be made to point at any possibly invalid thing, including nullptr ( 0 ).

However, there are smart pointers - but that I am guessing is probably too advanced at the moment.

Good Luck :+)
So my professor doesn't like the solution I gave to him.

"You do not need to pass parameters to the function. The variables a,b,c can be declared as local variables inside foo() much like you have been declaring variables inside main all quarter. The only thing communicated from foo() to main is the sum."

I tried using the int foo(int &D) function but it seems to not work the same way that the A, B, and C, function work. Any suggestions?


My attempted code: http://cpp.sh/96jy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Name: Jacob Chesley
// Date: 1/28/15
// Project: Function

#include <iostream>

using std::endl;
using std::cout;
using std::cin;


int foo()
{
	int A,
		B,
		C,
		D;
	cout << "Please enter three numbers: " << endl;
	cin >> A;
	cin >> B;
	cin >> C;
	D = A + B + C;
	return D;
}
int main()
{
	int Sum = foo();

	cout << "Your sum of these numbers is: " << Sum << "\n" << endl;

	return 0;
}


You were returning D as well as passing a reference to D for your argument. You weren't doing anything with what you returned. All I did with your code was I removed D as an argument, and kept where your function is returning D. In your main, I did int Sum = foo(); This is saying set the int sum to be equal to what the function returns, and since that function returns your sum, I think this is may be what you want.
Perfect thank you. Just to check, when I "int" a variable and then set it to equal foo, it will take whatever foo returned correct? This would also work the other way around if I returned something from main right?

Ill double check with my professor if this is what he wanted. I'm sure this is exactly what hes thinking.
Yes, but you should never call main from another function 99.9% of the time.
Thanks. I was just using it as an example because I only know of main and foo as a function.
Topic archived. No new replies allowed.