Functions
A function is a named block of code you can call with arguments. C passes every argument by value. What looks like an "out" parameter is a pointer the callee writes through.
Detailed Explanation
A function has a declaration and a definition. The declaration — the prototype — tells every caller the return type and the parameter types. The definition provides the body. Put the prototype in a header, include the header from both the caller and the defining .c, and the compiler can check the call against the definition. Without a prototype, C89 would invent an implicit int function and silently miscompile the call. C99 made that a constraint violation. Always declare before you call.
Arguments are copied. void bump(int n) { n++; } cannot change the caller's variable. void bump(int *n) { *n += 1; } can, because the pointer is copied and both copies address the same object. Arrays are the exception that is not an exception: an array argument decays to a pointer to the first element, so the callee receives an address, not a copy of the contents.
The return value is copied too. Returning a large struct is legal and, on modern ABIs, often passed via a hidden pointer the caller provides. Returning the address of a local is undefined behavior: the object is gone when the caller receives the pointer. Return a value, fill a caller-provided buffer, or allocate and document who frees.
A function with no parameters is declared void f(void), not void f(). The empty list in C means "an old-style declaration that does not specify parameters," which disables argument checking. C23 changed () to mean the same as (void), but code that must compile as C17 should keep writing (void).
static on a function gives it internal linkage. The name is invisible to the linker, so two translation units can each have a static void helper(void) without colliding. That is the default you want for helpers. Only export the functions that form your API.
C has no default arguments, no overload sets, and no keyword arguments. Two behaviors, two names, or one function that takes a flags argument. Variadic functions exist for the printf family and are a last resort: they disable type checking past the named parameters.
Code Examples
#include <stdio.h>
static int add(int a, int b) /* internal linkage: this file only */
{
return a + b; /* return is by value */
}
static void bump(int *n) /* out-parameter: write through the pointer */
{
if (n != NULL) {
*n += 1;
}
}
int main(void)
{
int x = 10;
bump(&x);
printf("%d %d\n", add(2, 3), x); /* prints: 5 11 */
return 0;
}#include <stdio.h>
#include <string.h>
struct Point {
int x;
int y;
};
/* Returning a small struct is fine. The ABI copies it. */
static struct Point midpoint(struct Point a, struct Point b)
{
struct Point m;
m.x = (a.x + b.x) / 2;
m.y = (a.y + b.y) / 2;
return m;
}
int main(void)
{
struct Point p = midpoint((struct Point){0, 0}, (struct Point){4, 6});
printf("%d %d\n", p.x, p.y); /* prints: 2 3 */
return 0;
}Common Gotchas
`void f();` is not `void f(void);` in C17 and earlier
the empty list skips argument checking. A call with the wrong arity will compile.
Returning `&local` gives the caller a dangling pointer
the object ends when the function returns. The compiler warns (`-Wreturn-local-addr`) for the direct case.
A missing prototype lets a call disagree with the definition
the linker may still succeed. Include the header from the defining `.c` so the compiler sees both.
There is no pass-by-reference
`const T *` is a read-only view, `T *` is a mutable view, `T` is a copy. Pick one on purpose.