Nearby lessons
73 of 124C - Function Definition
The function definition is the function's actual body — the code that runs when it is called. Learn the full syntax, how local variables live inside it, and the rules for returning a value.
The Full Syntax
A definition is a header identical to the prototype, followed by a body in braces — and no semicolon:
Local Variables Live and Die with the Call
Variables declared inside a function exist only while it runs. Each call starts with a fresh copy:
The return Statement
return does two things at once: it sends a value back and it exits the function immediately. Code after it never runs:
Every Path Must Return
If any branch can finish without a return, the caller receives garbage. Make sure a value comes back from every route:
void Functions
A void function returns nothing. You may still use a bare return; to leave early — a guard clause:
Returning Different Types
A function can return any single value type. To return several values, use pointers or a struct:
Where Definitions Can Go
Functions are defined at file level only. C does not allow nesting one inside another:
Type Conversion on Return
The returned value is converted to the declared return type. That silently truncates when types do not match:
Never Return a Local Array
A local array is destroyed when the function returns, so its address becomes invalid immediately. Have the caller supply the buffer instead:
Common Mistakes
| Mistake | Result |
|---|---|
| Semicolon after the header | Becomes a declaration; body is orphaned |
Missing return on a path | Caller gets garbage |
return value from void | Compiler error |
| Returning a local array's address | Dangling pointer |
| Expecting a local to persist | Value resets every call — use static |
| Defining a function twice | Multiple-definition linker error |
-Wall — warning: control reaches end of non-void function catches it before your users do.- A definition is the header plus a body in braces — no semicolon after it.
- Local variables are created on entry and destroyed on return.
- Every non-void path must return a value.
- return with no value is used to exit a void function early.
- A function cannot be defined inside another function in standard C.