+ 1
end of function?
I learned in class that at the end of a function such as ‘int main() {…}’ you have to place a return line. If it is an ‘int’ function it is expecting to return an int. Therefore most int functions that have nothing to return end with ‘return 0;’ as the last line in the function. In the exercises that I did so far, none of the functions had that line, and the line was not required to pass the exercises. Why?
2 odpowiedzi
+ 6
Quoted from https://en.cppreference.com/w/cpp/language/main_function
"The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;"
This of course, only applies to main() function. User defined functions are expected to explicitly return something - depending on their declaration return type. Functions with `void` return type are the only exception.
+ 2
https://code.sololearn.com/cNA9b1JO8hja/?ref=app
U mean this , well it's a special case where compiler assumes that main function has a return statement while in other functions it's mandatory