liveBook · Manning

Return statement

As explained in Modern C

A return statement is a C control-flow statement that ends execution of the current function and optionally supplies a return value.

A return statement ends execution of the current function and transfers control to the statement that called it. In main, `return EXIT_SUCCESS;` sends an `int` value back to the platform startup routine. When printf returns, execution resumes in main where the call stopped. section 2.4.3

Returning from a function

A function with a non-void return type must provide an expression at each return point. For example: bool leapyear(unsigned year) { return !(year % 4) && ((year % 100) || !(year % 400)); } The returned expression is converted to the function's declared return type when necessary. A function declared `void` may use `return;` without an expression. section 7.1

Key rules:

  • A function has one entry point but can contain several return statements. section 7.1
  • Match every return with the function declaration: value-returning functions require expressions, while void functions must omit them. section 7.1
  • Reaching the closing brace is equivalent to `return;` only for a void function; value-returning functions must not simply reach the end. section 7.1
Figure 2.1. Execution of a small program

Trace the calls from platform startup to main and printf, then follow each return to the point where execution resumes. section 2.4.3

How Modern C covers it

How deep it goes
Defines control transfer, typed expressions, multiple returns, and closing-brace behavior, but defers implementation internals.
Examples
Uses main returning EXIT_SUCCESS, printf returning to main, and leapyear returning a Boolean expression.
What it assumes
Assumes declared functions and basic types while establishing function calls and returns through concrete C examples.
What you can do after
Apply it via C syntax to return control and correctly typed values from functions.
How it connects
Connects function calls, function declarations, control flow, types, and the void type.

Also explained in

Compare how 5 books explain return statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.