Meaning of return expression in C

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Expression
AI Thread Summary
The discussion focuses on the C programming language's main() function, addressing its structure and return values. The main function can either take no arguments, written as main() or main(void), or accept command-line arguments using int main(int argc, char** argv). The term "void main" is discouraged as it implies no return value, while the correct form should return an integer, typically 0, indicating successful execution to the operating system. The return statement is essential for all functions, including main, as it communicates the function's output or status. In C, the distinction between functions and procedures is less pronounced than in other languages, with all callable entities referred to as functions. The return value from main is particularly important for scripts that may utilize it to determine the success or failure of the executed program. Overall, the conversation emphasizes best practices in C programming regarding function definitions and return values.
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 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 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 elusiveshame
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top