LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   return (https://www.linuxquestions.org/questions/programming-9/return-108530/)

linuxanswer 10-25-2003 09:37 PM

return
 
...
int main() {......
printf("hello");
return(0); // Why i should return 0 and not 1 or 2 ?
}

vanquisher 10-25-2003 10:05 PM

You can return 1 or 2...or any integer.

coolman0stress 10-25-2003 10:27 PM

Returning anything from main is usually insignificant. Though it's proper to return 0, since it tells the OS that the program terminated properly.

linuxanswer 10-25-2003 10:28 PM

but the meaning is the same? : return(0) return(1) return(n)

Jose Muņiz 10-25-2003 11:31 PM

main() is a function. A function is a piece of code that does certain tasks with information they receive, and that gives out certain information.

Suppose you are a boss and you tell one of your employees to do something, and you give him some materials to work with (arguments). He will then go and do whatever process he needs and will come to you with the result (returned to you via the return statement).

So... a function in C has the following parts:

variable_returned name_of_function(parameters)
{
return statement;
}

variable_returned indicates the type of variable that will be returned, say, an integer. Hence... in the function, if you return 3, for instance... the instance that called your function will receive value 3. Let's look at an example:

int returnsThree()
{
return 3;
}

int main()
{

int variable;

variable = returnsThree();
return 0;
}

The value of variable will be 3.

Now, in int main(), the return statement helps the operating system to know whether the program did a normal exit or not. Suppose you interrupted it... then, you will be able to see that its exit code is something different to 0... in that way, the return statement helps you to know what kind of exit took place.

I hope I was clear enough :P And sorry if there's any syntax error in my previous code :P I haven't programmed in C for like 3 months ;)


All times are GMT -5. The time now is 02:46 AM.