Just like in mathematics, in C++ functions return values. All functions in C++ must specify exactly what type of value they return, and every function must return only one type of thing. In some cases, that "one type of thing" might be nothing, which is denoted in C++ with the keyword void
.
Every function must declare what it returns. This is done via a function declaration. Here are several examples:
int foo();
void bar();
string baz();
int main();
4 function declarations. foo
returns an int
, bar
returns nothing, baz
returns a string
(which is declared in the C++ Standard Library), and main
returns an int.
Not only must every function declare what it returns, it must also return that type of thing. If your function returns void
, then you can write:
void bar()
{
return;
}
...or just do nothing:
void bar()
{
}
If your function returns anything other than void
, then you have to have a return
statement that returns that type of thing:
int foo()
{
return 42;
}
If you declare a function to return one type of thing, but then try to return another type of thing, then either there must be a way to implicitly convert from whatever you're trying to convert to what the function is declared to return. If there is no possible implicit conversion, your program won't compile. Consider:
int foo()
{
return "foobar rulez!";
}
Here, foo
is declared to return an int
, but I'm trying to return a string (not a string
from the Standard Library, but an old C-style const char*
string. `"foobar rulez!" here is called a string literal.)
It is possible to write code to provide the implicit conversion I mentioned earlier, but unless you know exactly why you want to do that it's better to not get mixed up in all that right now.
What do you do with the values that are returned from functions? Again, just like with mathematics, you can use those values somewhere else in your program.
#include <cstdlib>
#include <iostream>
int foo()
{
return 42;
}
int main()
{
int answer = foo();
std::cout << "The answer to Life, the Universe and Everything is...\n"
<< answer << "!\n";
return 0;
}
Obviously you can't do anything with the value that is returned from a function that returns void
, because a function that returns void
doesn't really return anything at all. But these kinds of functions are useful for doing stuff kind of on the side.
#include <cstdlib>
#include <iostream>
int theAnswer = 0;
void DeepThought()
{
theAnswer = 42;
}
int foo()
{
return theAnswer;
}
int main()
{
DeepThought();
int answer = foo();
std::cout << "The answer to Life, the Universe and Everything is...\n"
<< answer << "!\n";
return 0;
}
OK, back to all this business with main
.
main
is a function in C++. There are a few things about main
that make it special compared to other functions in C++, and two of those things are:
- Every program must have exactly one function called
main()
(in global scope).
- That function must return an
int
There is one more thing about main
that's a little special and possibly confusing. You don't actually have to write a return
statement in main
*, even though it is declared to return an int
. Consider:
int main()
{
}
Note that there's no return statement here. That is legal and valid in C++ for main
, but main
is the only function where this is allowed. All other functions must have an explicit return
statement if they don't return void
.
So what about the return value from main()
? When you run a program on an Windows or Linux computer, the program returns a value to the operating system. What that value means depends on the program, but in general a value of 0
means that the program worked without any problems. A value other than 0
often means that the program didn't work, and the exact value is actually a code for what went wrong.
Scripts and other programs can use these return values to decide what to do next. For example, if you wrote a program to rename an MP3 file based on the Artist and track Number, then your program might return 0
if it worked, 1
if it couldn't figure out the Artist, and 2
if it couldn't figure out the Track Number. You can call this function in a script that renames and then moves files. If you want your script to quit if there was an error renaming the file, then it can check these return values to see if it worked or not.
- no explicit return statement in
main
: In cases where main
does not have an explicit return
, it is defined to return the value 0
.