Return 0

closed account (N8MNAqkS)
Can someone show me a program or a few programs in which the return 0 function is used for something. I want to learn more about what it can do, if anything.
return exits the current function. if the current function has a type, it returns a value back to the caller.

int sum (int a, int b)
{
return a+b;
}

cout << sum(2,3);

you can use returns to exit early too, skipping unnecessary or even unwanted work.
void foo(char* cp)
{
if(cp == 0) return; //get out of here if its null
cp[0] = 0; //etc
}


return 0 in main is kind of a special case. OS design, all OS that I know of, allow programs to give them an exit code when the program ends. This is the code, and if its zero, that means 'all was well'. You can return 1 or any other value too, and the OS will think it exited abnormally and log that info somewhere. Early compilers and early OS let you actually use this in funky ways... I used to have a program that returned a double result of a computation and it worked great, in the dos era. Stuff is more restrictive now.
Last edited on
That would be impossible, because once main() returns, the program ends. By definition there can be no program that uses the return value of its own main() function.

The return value of main() indicates success or some error condition, and is typically used by shell programs (e.g. Bash, cmd.exe) to decide how to proceed.
For example, a shell script might do
1
2
3
4
5
6
7
result = execute('some_program file.txt')
if result != 0 then
    print('the program failed with error ' + result)
    execute('some_other_program')
else
    print('the program succeeded')
endif
The return value has no other function.

EDIT: I just realized I misread the question. I assumed HueMungus was talking about main(), but perhaps they weren't.
Last edited on
Topic archived. No new replies allowed.