Functions, Recursion, Scope, and Storage Classes
Write modular C programs using functions, understand parameter passing, recursion, and how storage duration and scope affect variable behavior.
Inside this chapter
- Why Functions Matter
- Function Declaration and Definition
- Call by Value and Addresses
- Recursion
- Scope and Storage Classes
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C basics to advanced memory, systems, debugging, and real-world development practice. Use the navigation at the bottom of each page to move smoothly through the full tutorial.
Why Functions Matter
Functions make programs modular, reusable, testable, and easier to understand. Good C programs avoid putting all logic in main. Instead, they divide responsibilities into clear units.
Function Declaration and Definition
#include <stdio.h>
int add(int a, int b);
int main(void) {
printf("%d\n", add(2, 3));
return 0;
}
int add(int a, int b) {
return a + b;
}
The declaration tells the compiler about the function before use. This becomes especially important in multi-file programs.
Call by Value and Addresses
C passes arguments by value. If you want a function to modify a caller variable, you typically pass its address through a pointer.
void increment(int *value) {
(*value)++;
} Recursion
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
Recursion is elegant for some problems, but students must understand base cases and stack usage carefully.
Scope and Storage Classes
| Keyword | Idea |
|---|---|
| auto | Default local storage duration |
| static | Persistent lifetime, limited scope depending on placement |
| extern | Reference to a global defined elsewhere |
| register | Historical hint for fast storage, mostly advisory |
Real-World Usage Snapshot
Functions and storage rules matter in every serious C codebase. Embedded firmware, networking utilities, database internals, and command-line tools all rely on carefully designed modular functions and predictable variable lifetime.