liveBook · Manning

Function

As explained in Modern C

A function is a named C program unit that can be invoked to perform computation and optionally return a value.

 5   bool leapyear(unsigned year) {
 6     /* All years that are divisible by 4 are leap years, 
 7        unless they start a new century, provided they 
 8        are not divisible by 400. */ 
 9     return !(year % 4) && ((year % 100) || !(year % 400));
10   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":19},{\"line\":0,\"ch\":27}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":13}],[{\"line\":4,\"ch\":38},{\"line\":4,\"ch\":40}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}]]"}
!@%STYLE%@!

`leapyear` receives an `unsigned` year, computes whether it is a leap year, and returns a `bool`. The code inside the braces is its definition. section 7.1

A function packages a computation so it can be called from elsewhere. A call such as `leapyear(2024)` supplies an argument; the function's parameter receives it. Library functions work the same way: `printf` accepts arguments that determine what it prints. TAKEAWAY: Put a feature in one function when it should be usable without copying its implementation. section 7.1 section 7.0

bool leapyear(unsigned year);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":14},{\"line\":0,\"ch\":22}]]"}
!@%STYLE%@!

This declaration gives the compiler the function's interface without repeating its body: a `bool` return type and one `unsigned` parameter. section 7.1

Parentheses enclose parameter declarations in a declaration or definition, but arguments in a call. A function prototype specifies parameter types and a return type, letting the compiler check and, where appropriate, convert values at a call. Write `(void)` for a function with no parameters; use `void` as the return type when it returns no value. `main` is the program's entry point. section 7.1 section 7.0

Functions make data flow explicit through arguments and return types. They also reduce repeated code and the editing errors it invites. Each call has its own local variables, even when a function calls itself recursively. TAKEAWAY: separate local variables do not imply separate pointed-to data; recursive calls can still modify the same data through pointers. section 7.0 section 17.4

How Modern C covers it

How deep it goes
Explains calls, prototypes, parameter and return types, void conventions, and per-call locals; treats pointer-mediated recursive state as a complication.
Examples
Uses printf output, a leap-year function, and a recursive function that passes nesting depth through a pointer.
What it assumes
Builds on earlier library-function calls and C declarations while introducing how to write functions and read their prototypes.
What you can do after
Build it from scratch: define a reusable C function, declare its interface, and call it with appropriate arguments.
How it connects
Connects function calls to prototypes, type conversion, modularity, recursion, local variables, and pointers.

Also explained in

Compare how 20 books explain function →

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.