Interview Q&A · C · 10 questions in this topic

Functions & Recursion — C Interview Q&A

All C topics

How C functions work — declarations, pass-by-value, recursion, and the stack-frame model behind call bugs. Part of Functions & Storage · 202 questions in the C bank.

How do function declaration, definition and calls fit together?

Answer

A declaration (prototype) gives the signature so callers compile; the definition provides the body exactly once; calls evaluate arguments and transfer control. Since C99, implicit declarations are illegal — every function must be declared before use, normally via a header.

Missing prototypes cause the classic implicit-int era bugs that -Wimplicit-function-declaration still hunts.

Example

Example.cc
/* mathutil.h: declaration */
int add(int a, int b);
/* mathutil.c: definition */
int add(int a, int b) { return a + b; }
/* main.c: caller includes mathutil.h */

💡 Declare in headers, define once, include before use — the complete function hygiene rule.

Does C have pass-by-reference?

Answer

No — C is strictly pass-by-value: every argument is copied. Reference-like behavior is simulated by passing pointers, letting the callee mutate through them. That is why scanf takes &x and why swap needs int*.

The discipline: pass small values directly, large read-only data as const pointers, and out-params as pointers with documented ownership.

Example

Example.cc
void swap(int *a, int *b) { /* simulated by-reference via pointers */
    int t = *a; *a = *b; *b = t;
}
void read_only(const int *p, size_t n); /* no copy, no mutation */

💡 No references, only values and pointers is the one-line C parameter model.

What is recursion, and what must every recursive C function have?

Answer

Recursion is a function calling itself on a smaller subproblem. Every correct recursion needs a base case that terminates and a progress guarantee toward it. Missing either gives infinite recursion and stack overflow.

C adds practical concerns: deep recursion exhausts the fixed call stack (prefer iteration for huge n), and naive exponential recursion (textbook Fibonacci) needs memoization or DP.

Example

Example.cc
long long fact(int n) {
    if (n <= 1) return 1;    /* base case */
    return (long long)n * fact(n - 1); /* progress: n shrinks */
}

💡 Always say base case plus progress in one breath — interviewers listen for both halves.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits