- Return 0; is superfluous - main (and only main) returns 0 by default when the end of the function is reached. Aug 2, 2010 at 5:11am mcleano (922) For people who are new to programming who may not know that it is a good habbit to get into.
- GameDev.net is your resource for game development with forums, tutorials, blogs, projects, portfolios, news, and more.
A return
statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return
statement can return a value to the calling function. For more information, see Return type.
Hi, First a introduction: Im kinda new to C, i actualy began programming in QBasic:D hehe. Then stopped and started with PHP/MySQL - asp JS and more web languages. Now i pointed my arrow at game dev, starting C Bought some books and started with Tutorials. So i stumbled upon Rachel, who has.
Syntax
jump-statement:return
expressionopt;
The value of expression, if present, is returned to the calling function. If expression is omitted, the return value of the function is undefined. The expression, if present, is evaluated and then converted to the type returned by the function. When a return
statement contains an expression in functions that have a void
return type, the compiler generates a warning, and the expression isn't evaluated.
If no return
statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined. If the function has a return type other than void
, it's a serious bug, and the compiler prints a warning diagnostic message. If the function has a void
return type, this behavior is okay, but may be considered poor style. Use a plain return
statement to make your intent clear.
As a good engineering practice, always specify a return type for your functions. If a return value isn't required, declare the function to have void
return type. If a return type isn't specified, the C compiler assumes a default return type of int
.
Many programmers use parentheses to enclose the expression argument of the return
statement. However, C doesn't require the parentheses.
The compiler may issue a warning diagnostic message about unreachable code if it finds any statements placed after the return
statement.
In a main
function, the return
statement and expression are optional. What happens to the returned value, if one is specified, depends on the implementation. Microsoft-specific: The Microsoft C implementation returns the expression value to the process that invoked the program, such as cmd.exe
. If no return
expression is supplied, the Microsoft C runtime returns a value that indicates success (0) or failure (a non-zero value).
Example
This example is one program in several parts. It demonstrates the return
statement, and how it's used both to end function execution, and optionally, to return a value.
The square
function returns the square of its argument, in a wider type to prevent an arithmetic error. Microsoft-specific: In the Microsoft C implementation, the long long
type is large enough to hold the product of two int
values without overflow.
The parentheses around the return
expression in square
are evaluated as part of the expression, and aren't required by the return
statement.
The ratio
function returns the ratio of its two int
arguments as a floating-point double
value. The return
expression is forced to use a floating-point operation by casting one of the operands to double
. Otherwise, the integer division operator would be used, and the fractional part would be lost.
The report_square
function calls square
with a parameter value of INT_MAX
, the largest signed integer value that fits in an int
. The long long
result is stored in squared
, then printed. The report_square
function has a void
return type, so it doesn't have an expression in its return
statement.
The report_ratio
function calls ratio
with parameter values of 1
and INT_MAX
. The double
result is stored in fraction
, then printed. The report_ratio
function has a void
return type, so it doesn't need to explicitly return a value. Execution of report_ratio
'falls off the bottom' and returns no value to the caller.
The main
function calls two functions: report_square
and report_ratio
. As report_square
takes no parameters and returns void
, we don't assign its result to a variable. Likewise, report_ratio
returns void
, so we don't save its return value, either. After each of these function calls, execution continues at the next statement. Then main
returns a value of 0
(typically used to report success) to end the program.
To compile the example, create a source code file named C_return_statement.c
. Then, copy all the example code, in the order shown. Save the file, and compile it in a Developer command prompt window by using the command:
cl /W4 C_return_statement.c
Dev C Error Main Must Return Intervals Calculator
Then, to run the example code, enter C_return_statement.exe
at the command prompt. The output of the example looks like this: