[Courses] C Programming For Absolute Beginners, Lesson 5A: All About Functions, Part 2
ssd532 at gmail.com
Mon Apr 2 19:07:34 UTC 2012
> What is the difference between:
> void functionname (void)
> int functionname (void)
> Why is it OK to use 'void functionname (void)' for ordinary functions,
> OK to use 'void main (void)' ?
Defining return type of main() as int allows a return code to be passed to
i.e. the user or the calling process. Users can refer this exit status for
Or invoking programs can use this return code for something useful
And as it is required by standard, not defining return type as int can
Refer following page on avoiding void main(void).
Excerpt from the page:
Because you are likely to return a random value to the invokation
environment. This is bad, because if someone wants to check whether your
program failed, or to call your program from a makefile, then they won't be
able to guarantee that a non-zero return code implies failure.
(To which the answer is usually of the form "that's their problem").
This page demonstrates a system on which a void main(void) program will
very likely cause problems in the third class above. Calling the program
from a script may cause the script to die, whether or not its return code
is checked. Calling it from a makefile may cause make to complain. Calling
it from the command line may cause an error to be reported.
Ordinary functions are not special functions like main(). So using void
functionname (void) will
not have much problems. And returning int for every such trivial function
will also be a headache.
More information about the Courses