Meaning of return expression in C

  • Thread starter fog37
  • Start date
  • Tags
    Expression
In summary, the main() function in C programming does not require any arguments, but it can take them if needed. The expression void main is not the same as main(void) and is considered a bad coding habit. The main function must have a return statement at the end, and the value returned is typically 0 to indicate a successful execution. This value can be used in command scripts to determine the program's success or failure. The terms "function" and "procedure" do not have a strict distinction in C programming.
  • #1
fog37
1,568
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
  • #2
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 JorisL
  • #3
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 elusiveshame and meBigGuy
  • #4
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 elusiveshame

FAQ: Meaning of return expression in C

1. What is the meaning of a return expression in C?

A return expression is a statement in the C programming language that is used to exit a function and return a value to the caller. It is typically used to pass the result of a function's computation back to the calling code.

2. How is a return expression used in C?

A return expression is used at the end of a function to indicate that the function has completed its task and is ready to return a value. It can also be used at any point within a function to exit the function early if a certain condition is met.

3. Can a return expression have multiple values in C?

No, a return expression can only have a single value in C. If a function needs to return multiple values, it can either use pointers or structures to pass them back to the caller.

4. What happens if a function does not have a return expression?

If a function does not have a return expression, it will still compile and run without any errors. However, the function will not have any return value, and the calling code will not receive any information from the function.

5. Can a return expression be of any data type in C?

Yes, a return expression in C can be of any data type, including integers, characters, floating-point numbers, and pointers. It can also be of user-defined data types, such as structures and arrays.

Similar threads

Back
Top