Reputation: 33
For context, the language I've been using to teach myself to code is Python. Now I'm trying to learn C++ to broaden my coding literacy, but I'm having trouble understanding how functions are returning values. I've been reading the following documentation: https://www.cplusplus.com/doc/tutorial/program_structure/
Perhaps I need a better understanding of returning a value vs outputting something, but what is confusing me about the first program structure exercise is that it starts off with the classic "Hello world" example, which is obviously a string. However, the "int main()" is expecting an integer, not a string, right? So wouldn't there be some sort of error that would prevent the program from compiling?
Upvotes: 1
Views: 1008
Reputation: 6684
It seems the confusion here is not about the main
function primarily, so let's step away from main
for a moment.
Printing a value and returning a value are fundamentally two different things that really don't have anything to do with one another. The confusing part is we often use the word "output" to mean one of several different things.
Let's start with understanding what a return value is by itself. So hopefully you know that functions can be called from inside other functions and serve as a way to organize the functionality within a program. Let's look at a very simple example:
int getDoubled(int x)
{
return 2 * x;
}
Here we've defined a simple function with the name getDoubled
and it expects, as an argument, a single integer and returns a single integer. And we can see from the code that the integer returned is x
, the input argument, multiplied by 2.
Now that we have a function we can call it from somewhere else:
int y = getDoubled(3);
Here we've called the function by writing its name followed by a parameter list. This means the code inside the function will be run and the expression in its return statement will be the value that it acts like. In this case that means y
is assigned the value 6
, because getDoubled(3)
evaluates to its return value of 6
.
Notice this has nothing to do with printing the value 6 to the screen, the return type and value of a function in C++ is only for determining the value the function call represents in an expression. Consider this now:
int y = getDoubled(getDoubled(3));
Now y
will be assigned the value 12 because the inner getDoubled
call returns 6, which is passed as the parameter to the outer getDoubled
call which then returns 12.
Since function calls are just expressions they can be used anywhere an expression is expected, like in an if
statement:
if (getDoubled(y) < z) { /* ... */ }
In fact, we can even use the bool
return type to write functions we might call directly in an if statment:
bool isLess(int x, int y)
{
return x < y;
}
So now we could write something like:
if (isLess(z, 5)) { /* ... */ }
Admittedly doing this is pretty pointless since you could also just write z < 5
in there. This is just to illustrate how this works without getting bogged down in irrelevant details.
So the return type describes the type of value that a function call will evaluate to.
Now, with all that said, main
is actually very special. It's not an ordinary function, because you're actually not permitted to call main
yourself and you're even allowed to omit its return statement. The reason for this is that the operating system, which is going to run your program, needs some entry point to start running from.
Unlike Python, C++ doesn't allow "executable" code at the top level of a program, it only allows definitions and declarations (there's some wiggle-room here for initializing static variables and the like, but we'll ignore that for now). My point here is you can't just write this as a program in a cpp file:
std::cout << "Hello world!\n";
C++ requires that these sort of statements only appear inside functions. So when the operating system goes to execute your program, where does it start? This is what the purpose of main
is. It marks the entry point for your program. It's as if the operating system calls that main
function whenever your program is run by a user.
This is in contrast to Python where the entry point of your program is simply the script file that was invoked. There is still technically a main
, but that's inside the python
executable itself and you don't have to worry about it there.
As other answers point out, the return value of the main
function is purely for the purposes of the operating system to understand whether your program completed successfully or failed for some reason. And for that purpose it uses an int
.
Okay, so with that out of the way, what's the deal with printing and cout
? Well, this is another part of the interface between your program and the operating system. Your program actually has what are called standard streams. Usually there's 3 of them: standard output, standard input, and standard error. These are all provided in C++ as cout
, cin
, and cerr
, respectively.
When you write to cout
you're putting data into the standard output stream of your program. Any part of your program can do that. That standard output stream usually prints to the console if you've created a console application. But operating systems usually let you send that output stream to other places, like a file, or even connecting it to the input of another program.
One way to think of it is like a special file that you can write to (and likewise for cin
that's a different special file that you can read from). It's one way to get data out of your program and up to where the user can see it, but it's entirely different mechanism to returning from a function.
Upvotes: 2
Reputation: 76285
main
returns int
. Nothing else.
There are three portable return values from main
: EXIT_FAILURE
, EXIT_SUCCESS
, and 0
. Returning EXIT_SUCCESS
or 0
returns an implementation-specific value that the OS will interpret as successful execution. Returning EXIT_FAILURE
returns an implementation-specific value that the OS will interpret as failed execution. You get those macros from <cstdlib>
or from <stdlib.h>
.
Most of the time the value doesn't matter; it's simply ignored. But when you run a script through the OS, sometimes it matters whether a particular program succeeded, and then you write code in the script to check the value that the program returned. A simple example (well, simple for a shell script, but still somewhat cryptic):
./my_program
if [ $? -eq 0 ];
then
echo "Succeeded"
else
echo "Failed"
fi
Here, $?
is the result of the previous command. Since the previous command was ./my_program
, if the program ran successfully $?
will be 0, and if not, it will be some other value.
Upvotes: 3
Reputation: 418
The main() function has an an int return code on the most operating system environments. The return code is the program exit code. A value of 0 means no error, other values are mostly interpreted as errors. However, on an OS less embedded applications (bare metal) the main function never returns and therefore the function is declared as void main().
Upvotes: 0