Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Meaning of return expression in C

  1. Jun 6, 2015 #1
    Hello Forum,

    I am new to C programming and I have a few questions about the main() function.

    The main function does not take any arguments (nothing written between the round parentheses): main() or even main(void) . I have also seen the expression void main. Is that the same as main(void)?

    Every function, main included, must have a return statement at the end. For the main function, the return=0. What does that mean? Why 0? My understanding of the return statement is that it reports the output value of the function. If a function job is to calculate the sum of two integers, i.e. sum = a+b, then return = sum

  2. jcsd
  3. Jun 6, 2015 #2
    The main function can take arguments, if you so desire. If you want to be able to execute the program from a command line with specific arguments, the standard way of writing main would be: int main(int argc, char** argv): http://www.cprogramming.com/tutorial/c/lesson14.html

    The expression void main means that it has no return statement (it is not the same as main(void), which implies that it takes no parameters). It is considered a bad coding habit and should be avoided. Main should always return an integer. Most compilers will forgive you for this though and will not give a syntax error.

    Functions which start with void don't need a return statement, that's what the void in front of the name means: that it returns nothing. For the main function, the return type should be int and in general, the value upon a successful execution of the main function is 0. This value is sent to the operating system so it knows the program terminated successfully.
  4. Jun 6, 2015 #3


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    Usually the term "function" is used when there is a single value returned. In that case, a return statement is necessary. Many people consider it bad form for a function to do anything except calculate the value that it returns. We say that it has no hidden "side effects". The term "procedure" often means that there is more than just a single returned value. A procedure can do a lot of other things in addition to returning a value. A procedure does not have to have a returned value. When it does, it must have a return statement. The value returned by a procedure is often a status flag that tells you if it succeeded in it's primary task. That is the normal use for a returned value from main. If you call your program from a command script, the returned value from main can be used to determine if the program succeeded or failed. Then your script can take appropriate action.

    PS. I should add that a lot of this is personal preference for what I consider good programming. I don't think that there are any authoritative rules on how these terms are used.
    Last edited: Jun 6, 2015
  5. Jun 6, 2015 #4


    Staff: Mentor

    This is terminology (procedure) that isn't used in C. Pascal distinguished between subprograms that returned a value (a function) and procedures, which cause something to happen but don't explicitly return a value. Fortran makes the same sort of distinction between functions and subroutines.

    C and C++ don't make this distinction. You can have a function that returns a value of some type, or a function that has no return value, a void function, whose prototype looks like "void function_name(<arg list>)"
    The OP asked about C, which has only the function concept. We call a function that returns a status, a function.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook