# int (main) why do we need to return a value?

This topic is 3582 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

what is return a value? I understand what (main) is, but I'm confused to understand int. Is int function like store integer?

##### Share on other sites
The purpose of a function is to return a value. A function takes some input, does the required processing on that input, and then returns the output. If you make a function to add some numbers together, you need to get the answer back. That said, any program itself, is called like a function of the OS, and returns a value back.

This value is usefull so that the whatever called the program can find out if the programed terminated successfully or not. Because, sometimes you call programs from a batch file, or from withing another program, and you need to know to abort incase something went wrong.

Simply use a return 0; at the end of your main function.

int AddNumbers(int n1, int n2){    return n1 + n2;}int answer = AddNumbers(10,12);

##### Share on other sites
There are a few reasons.

1. It's a standard to use int main()
2. Often times, it depends on how the program is used. The calling application (if you launch your program from a script) might expect a return 0 which indicates the program terminated normally. Anything else would usually mean there was an error.

more on this here:

##### Share on other sites
Thank you very much. By declaring int, we can check the program. I use Dev C++. When I choose compile&run and declare int, the software can find error. If I didn't declare int, the software didn't find error. Am I right?

##### Share on other sites
That's because your compiler looks for an "int main()" function.
"main()" is not a valid function declaration.
And "void main()" is a different function because it has a different type signature than "int main()", so the compiler complains.
Note that some compilers may accept "void main()".

By the way, I suggest you to use Code::Blocks, because Dev-C++ is not actively updated anymore (last one was in February 2005).

##### Share on other sites
The C language was created as a way to write the Unix operating system in a portable manner back when the Beatles were still releasing new material. As a result, many if not most applications for Unix were also writtent using the C language, since there it was available.

One of the basic paradigms of the Unix operating system is that there are many small tools that operate on their standard input stream, perform some simple manipulation, and forward the results to their standard output stream. These smaller tools are tied together into an assembly in which the whole is greater than the sum of its parts. This is the origin of the concept of a standard in and standard out and of other Unix concepts like pipes to connect those streams between programs, and the "shell" that can be directed, via a script, to tie all those small programs together into a functional whole.

Some times one of those small Unix tools encounters a problem and cannot effectively complete its processing task. For example, a disk failure or a syntax error in a text stream being processes (after all, humans need to be involved at some point, and humans err). The designers of the shell determined that the best way for these tools to indicate that there was a failure condition was for the tool to return a status value to the invoking environment. That meant that the C language was designed so that upon return from the entry point it would need to leave an integral status value on the top of the stack that could be examined by the shell and appropriate action taken upon failure. A value of 'false' (zero) meant that no failure took place, and any other value was a failure.

By the time, decades later, when other platforms began to use the C language and its descendants, the requirement of returning a status value to the invoking environment from the program entry point was not only well established, but required by the various language standards. In fact, in C++ a conforming implementation will return a zero status value by default, even if you don't explictly return a value at all (C does not, much to the confusion of many beginners who don't understand why their shell script exits on failure when their program obviously succeeded). This status value does not always make any sense given the architecture of the operating system on which a program may be invoked, but like many a result of evolution, it's still there and sometimes gets infected and has to come out.

That, in a nut shell, is why main() has to return an int value in C and descendant languages, even if it doesn't make sense to someone unfamiliar with history.

--smw

##### Share on other sites
main() {...} is a valid function declaration in C (although not C++ iirc). The return type defaults to int.

Relying on the int default is not good style, and function declarations should always be given explicit return types. Nevertheless, it is valid code.

##### Share on other sites
Quote:
 Original post by gsgmain() {...} is a valid function declaration in C (although not C++ iirc). The return type defaults to int.

Implicit types are not allowed in C99 and were never allowed in C++. I don't believe void main() was allowed by any standard C-family language.

--smw

##### Share on other sites
Quote:
Original post by Bregma
Quote:
 Original post by gsgmain() {...} is a valid function declaration in C (although not C++ iirc). The return type defaults to int.

Implicit types are not allowed in C99 and were never allowed in C++. I don't believe void main() was allowed by any standard C-family language.

It may depend on what is implied by "allowed". Looking up the draft standards, it sounds like C89 (which is perhaps more relevant) only allowed for int main(void) and int main(int, char **) (assuming hosted imlementations) while C99 also allows "implementation defined" versions. Using implementation defined features means it's no longer a "strictly conforming program", but it's still a "conforming program".

On the other hand, having two functions with external linnkage named foobar1 and foobar2 is not strictly confomring C89 since you can't rely on having more than 6 significant initial characters in an external identifier. C99 increased that to 31.

##### Share on other sites
Quote:
 Original post by BregmaImplicit types are not allowed in C99 and were never allowed in C++. I don't believe void main() was allowed by any standard C-family language.

void main() is wrong, but main() is valid C89. If the OP is compiling C instead of C++, that has some bearing on his second question.