Amartya Gupta Amartya Gupta - 26 days ago 19
C Question

Return 1 in non main function

I am currently learning c and i have a confusion regarding return:

int factorial (int n)
{

if (n == 1)

return 1;

else

return n * factorial (n -1);

}


In the above recursive code in the last call when n is 1, will the return 1 will return the integer value one or error in execution like in main. I had this confusion because return 1 is treated differently in main and in a function called by main...

Answer

Will the return 1 return the integer value one or error in execution like in main?

return 1 returns the integer value 1 from any function declared to return int, even if that function happens to be int main().

A non-zero return value from main() is interpreted to indicate an error in execution. That happens to be the semantic of the return value of main().

Comments