Meaning of return expression in C

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Expression
Click For Summary

Discussion Overview

The discussion revolves around the meaning and usage of the return expression in the C programming language, particularly in the context of the main() function. Participants explore the implications of different return types, the necessity of return statements, and the distinction between functions and procedures.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants note that the main function can take arguments, while others emphasize that it is commonly seen without arguments, such as main() or main(void).
  • There is a discussion about the expression void main, with some arguing it is not the same as main(void) and is considered poor practice.
  • Participants mention that every function, including main, must have a return statement, with a return value of 0 typically indicating successful execution.
  • Some participants discuss the distinction between functions and procedures, suggesting that functions are expected to return a single value while procedures may perform additional tasks without necessarily returning a value.
  • There is a reference to terminology differences in other programming languages, such as Pascal and Fortran, which distinguish between functions and procedures, while C does not make this distinction.
  • Some participants express that the use of return values, especially from main, can serve as a status flag for program execution, which can be useful in scripting contexts.
  • There is a personal preference expressed regarding programming practices, with some participants indicating that there are no authoritative rules governing these terms.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of a return statement in the main function and the common return value of 0 for successful execution. However, there is disagreement regarding the terminology used to describe functions and procedures, as well as the implications of using void main.

Contextual Notes

The discussion highlights varying interpretations of programming terminology and practices, with some participants noting that these interpretations may be influenced by personal preferences rather than established rules.

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
 
Technology 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

Similar threads

  • · Replies 17 ·
Replies
17
Views
10K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K