I think you need to take a step back Fyah. You are flailing in the process to make sense of something you do not fully understand, and are over-complicating things. Don't worry, we've all been there at some point.
Let's just forget everything we
THINK we know about void and return types, ok? :)
Keep in mind, there is no one right way of doing things. Let's keep things simple for now.
What is return?
The return statement is used inside a function body and does the following things :
•Terminates the execution of a function.
•If an expression is present, it returns the expression to the caller.
•Transfers control back to calling function.
What is a return type?
A return type is a type (class, integral type, etc.) which you prepend to a function definition, to signify that you want this particular function to return an expression of said type.
Quite a bloated definition (sorry), take a look below.
Example of what I just said :
1 2 3
|
int returnInteger() {
return 10;
}
|
In this case, returnInteger() returns 10.
It's a redundant function, but for the sake of demonstration this is how one might use this function :
1 2 3 4
|
int main() {
int foo = returnInteger(); // foo is now 10.
return 0;
}
|
Why does main have an integer return type? And why does it return 0?
I'll try to touch on this quickly.
When writing a function, it is up to the programmer to decide what the purpose of their function is. This means that functions must be used within a certain context where they and the expressions they return make sense to be used. For instance, I could write a function called
int returnAge()
which might return the age of a person, or I could write a function called
int returnIndex()
which would return an index to be used with a container. Both functions return an
int
, but each returned expression has a unique context in which an integer can mean different things.
Take everything I say with a grain of salt, I don't want to confuse you further. The actual answer to this question is that main's return expression can be used to return different error codes to the operating system when the program terminates. This is mostly useful for debugging code, but you should engrave this in your memory as a good habit.
In the example above, main returns 0 to signify that it has terminated successfully.
When would I want to use void
as a return type?
void
is used when you don't want to return an expression.
Example :
1 2 3
|
void sayHello() {
std::cout << "Hello!" << std::endl;
}
|
Again, keep in mind that there are many ways to skin a cat. Sometimes you can do the same thing using different methods.
The following functions effectively do the exact same thing with a little change in syntax :
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int input() {
int i=0;
std::cin >> i; //user enters number for i.
return i; //i is returned.
}
int main() {
int foo = input();
std::cout << foo << std::endl;
std::cin.sync();
std::cin.get();
return 0;
}
|
And the same function using
void
and references :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void input(int& i) {
std::cin >> i; //user enters number for i.
//there is no return statement because input() is void and...
//i is a reference.
}
int main() {
int foo=0;
input(foo); //foo passed as reference and modified in input()
std::cout << foo << std::endl;
std::cin.sync();
std::cin.get();
return 0;
}
|
I hope this clarifies return types and how to use them. I will edit this as needed.