Sorry for the late response.
I don't think I changed from "=" to "==". Did you make an error in your post? |
I assumed you went back and edited your original post to change it from '=' to '==' after Homberto pointed it out.
I don't get the difference between void and main |
I think you meant, void and int, yes?
Every function must have a return type, such as void or int or any other type you can think of.
It's up to the programmer to decide what to do with the things returned from functions. Just because a function returns something doesn't necessarily mean that it will be printed. If the programmer wants to print the returned value of a function, then it's up to them to make it happen.
What makes a
void
function unique (as you already pointed out) is that it returns nothing. You'd use
void
as a function return type when you don't want a function to return something:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
void sayHello() {
std::cout << "Hello" << std::endl;
}
int main() {
sayHello();
std::cin.get();
return 0;
}
| |
'sayHello' is a
void
function because it doesn't return anything. It simply prints "Hello".
If, on the other hand, I did something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include <string>
std::string returnHello() {
std::string helloString = "Hello";
return helloString;
}
int main() {
std::cout << returnHello() << std::endl;
std::cin.get();
return 0;
}
| |
The end effect is the same. The program prints "Hello". The differences being:
1.) There is no longer a
void
'sayHello' function, instead we have a 'returnHello' function which returns a
std::string
object.
2.) The actual printing occurs in the main function, which makes a call to 'returnHello'.
As you can see, it is up to the programmer to make use of the values returned by functions. If functions are written in such a way that they return something (in other words, they are not
void
), one can assume that the result is important, otherwise, why should the function return anything?
However, it is entirely possible to simply ignore the values returned by functions. You don't need to do anything with returned values, but it's a strange thing to do since you, as the programmer, wrote the function in such a way that it returns something.
The following is entirely valid, but pointless.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
std::string returnHello() {
std::string helloString = "Hello";
return helloString;
}
int main() {
returnHello();
//returned string object isn't being used for anything. Is effectively discarded and lost forever.
//nothing happens
std::cin.get();
//the program ends
return 0;
}
| |
That was some preliminary stuff. Now, on to the main function.
The C++ standard dictates that the main function must have a return type of
int
. The reason for this is that it allows a programmer to indicate whether or not a program executed successfully:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
bool there_was_an_error() {/*check for errors*/}
int main() {
if (there_was_an_error()) {
//Oh no! Let's return 1 to indicate a failure.
return 1;
}
else {
//Yay! Everything went smoothly.
return 0;
}
}
| |
Note that this program is incomplete, because the function 'there_was_an_error' doesn't return anything yet. I left it empty and put a comment there because this is simply pseudo-code. We can assume that the 'there_was_an_error' function returns
true
when there was an error, and
false
when there was none. What errors it's checking for is entirely arbitrary and irrelevant.
In the program above, the programmer decided to indicate a failure with "1" and success with "0". These numbers are also arbitrary, and only mean something to someone or something that knows what these numbers represent - in this case, whether or not the program executed successfully. When used like this, these numbers go by a variety of names such as "return codes", "error codes" or "exit codes" - these names all basically mean the same thing.
One example where you might experience error codes in action is with an installation wizard. If you're installing a program, but something goes wrong, the setup will return a "failure" error code to the operating system. I don't know which operating system you're using, but when that happens to me on my windows machine, the operating system responds to that kind of failure error code with a "Did this install correctly?" dialog box.
In reality, there is no real reason for the main function to return integers specifically, it's pretty arbitrary. I suppose it's just that integers lend themselves to representing error codes, since they are numbers - it's also just what the standard decided on.