Meaning of return expression in C

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Expression
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
fog37
Messages
1,566
Reaction score
108
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

Thanks,
fog37
 
Physics news on Phys.org
fog37 said:
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)?

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.

fog37 said:
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?

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.
 
  • Like
Likes   Reactions: JorisL
fog37 said:
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.
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:
  • Like
Likes   Reactions: elusiveshame and meBigGuy
FactChecker said:
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.
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>)"
FactChecker said:
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.
The OP asked about C, which has only the function concept. We call a function that returns a status, a function.
FactChecker said:
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.
 
  • Like
Likes   Reactions: elusiveshame