int showVal(int, int, int ); // function prototype
int x = 3;
int r = 3;
int y = 6;
int z = 5;
showVal(x, y, r);
int showVal(int x, int z, int r)
cout << 0;
/** What prints out?**
i get that int main() will go to showVal and print 0 out, but then it says return 1; what is that and what does it mean? Also it happens again in showVal - return 2; What are these return 1; return 2; things and what do they do???????
HELP WITH "RETURN" in c++!!
Post a Reply
|Oldest Newest Rating|
· April 29, 2015
All that would print out would be 0 because you only used cout on zero.
Return means to give back a value for a function after it is called. It might help to think of it more mathematically. For example take the function f(x)=2x. This means given an input x, the value generated would be 2*x.
Translated into C++, f(x)=2x becomes
int f(int x)
This value could then be assigned into another variable.
Cout is a function that prints something out to your screen. Everything else just loads and stores value. Like I said, when a function returns something, it means given an input, it gives back a corresponding output which can be put into a local variable. When you say return 2, you mean that if you assign a variable to be the result of that function, that variable would have the value 2.
Void functions are a bit different because they don't return anything. They are typically used when you want to compress your code by making a void function do specific operations. This also allows easier debugging and avoids making it hard to figure out what you are doing in code. Voids are either I/O or doing work in summary. In C++, void functions could also be modifying a variable by reference.
In the main function, you should return 0 by convention since otherwise, it usually indicates some sort of error occurred. If an error actually did occur, you want to return a value other than zero indicating the type of error.
I would highly recommend watching Bucky's videos for an introduction to C++. I think I answered your question, but he goes into much more detail.
Used in many types of software including music players, video games, and many large scale applications.
|Bucky Roberts Administrator|