Nearby lessons
80 of 124C - Types of Functions
C functions fall into clear categories — library versus user-defined, and four combinations of "takes arguments" and "returns a value". Learn all four forms and when each is the right choice.
Library vs User-Defined
The first and broadest division:
| Header | Provides | Examples |
|---|---|---|
<stdio.h> | Input and output | printf, scanf, fopen |
<string.h> | String handling | strlen, strcpy, strcmp |
<math.h> | Mathematics | sqrt, pow, sin |
<stdlib.h> | Utilities and memory | malloc, atoi, rand |
<ctype.h> | Character tests | isalpha, toupper |
Form 1 — No Arguments, No Return
A self-contained action. Useful for banners, menus and fixed messages:
Form 2 — Arguments, No Return
Takes input, produces an effect rather than a value. Typical for display routines:
Form 3 — No Arguments, Returns a Value
Produces a value from somewhere other than its parameters — input, a global, or a constant:
Form 4 — Arguments and a Return Value
The most useful form: inputs in, result out, no side effects. Easy to test and easy to reuse:
The Four Forms Summarised
| Form | Signature | Typical use |
|---|---|---|
| No args, no return | void f(void) | Banners, menus |
| Args, no return | void f(int) | Printing, updating |
| No args, returns | int f(void) | Reading input, constants |
| Args and returns | int f(int) | Calculations — prefer this |
Recursive Functions
A function that calls itself. Every recursive function needs a base case that stops the descent:
static Functions — File-Private
A static function is visible only inside its own .c file. Use it for helpers that are not part of your module's public interface:
Functions Grouped by Purpose
A practical way to organise a program: pure calculations, input, output, and validation each in their own group:
Common Mistakes
- Mixing calculation with printing — a function that computes and prints cannot be reused where you need only the number.
- Recursion with no base case — the stack fills and the program crashes.
- Using globals instead of parameters — makes the function untestable and order-dependent.
- Making everything
void— returning a value is usually more flexible. - Forgetting the header — using
sqrtwithout<math.h>.
void showArea(float l, float w) that prints internally is stuck: you cannot sum areas, store them, or unit-test them. Write float area(float l, float w) and let the caller decide what to do with the result.- Library functions come from headers; user-defined ones you write.
- The four forms: no args/no return, args/no return, no args/return, args/return.
- The args-and-returns form is the most useful and most testable.
- A recursive function calls itself.
- static limits a function to its own source file.