Nearby lessons
74 of 124C - Function Calling
Calling a function transfers control to it, runs its body, and brings the result back. Learn call syntax, using the returned value, nested and chained calls, and how the call stack keeps track of it all.
The Basic Call
Write the name, then the arguments in parentheses. The parentheses are mandatory — without them you get the function's address, not a call:
A Call Is an Expression
Because a non-void call produces a value, it can go anywhere a value can go:
Nested Calls
An argument can itself be a call. C evaluates the inner call first and feeds the result outward:
The Call Stack
Ignoring the Return Value
C lets you discard a return value. Sometimes that is fine; sometimes it hides a failure you needed to know about:
Arguments Are Copies
By default C passes arguments by value — the function works on copies, so the caller's variables are untouched:
Calling in a Loop
A function called in a loop runs fresh each iteration — this is where reuse really pays off:
A Function Calling Itself
Recursion is just a function calling itself. Each call gets its own frame, and a base case stops the descent:
Common Mistakes
- Omitting the parentheses —
sayHello;compiles but calls nothing. - Wrong argument count — passing two arguments to a one-parameter function is a compile error.
- Arguments in the wrong order —
divide(2, 10)instead ofdivide(10, 2)compiles and gives the wrong answer. - Expecting the caller's variable to change — pass a pointer if the function must modify it.
- Ignoring a status return — a silently failed
fopenorscanfis a hard bug to trace. - Recursion with no base case — the stack fills and the program crashes.
int, divide(2, 10) is perfectly valid code that returns 0 instead of 5. Name parameters clearly, keep the count small, and consider a struct when a function needs more than three or four related inputs.- Syntax: functionName(arguments); — parentheses are required even when empty.
- A call to a non-void function is an expression with a value.
- Arguments may themselves be function calls.
- Each call gets its own stack frame holding its locals.
- Ignoring a return value is legal but often a bug.