Program run without return

Apr 1, 2019 at 3:42pm
The program runs fine without any return i wanna know what's return statement do exactly?
Apr 1, 2019 at 3:46pm
It returns the value to the caller. If you leave out the return statement from main it will automatically return 0 (meaning the program ended correctly). If you leave out the return statement from a non-void function other than main the behaviour is undefined which means that there is no guarantees what will happen. It might work, but don't rely on it.
Apr 1, 2019 at 3:46pm
return sends a value back to the caller and exits the current function.

int foo()
{
return 3;
}

x = foo(); //return 3 sends the 3 back and it will land in x.


in main (and only in main), return sends the value back to the *operating system* and exits the program.


in void functions, it exits the function only, no value is sent.
Topic archived. No new replies allowed.