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?
// 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;
}
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?
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?
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.
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?
// 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.