C Functions Formula Sheet and Revision Notes: Prototypes, Scope and Recursion

Revise C functions through compact syntax cards, when-to-use cues and exact traces for value copies, pointer mutation, static locals, recursion and callbacks.

KnowledgeGate Team

Exam prep & CS education

Updated 24 Nov 20266 min read
C Functions Formula Sheet and Revision Notes: Prototypes, Scope and Recursion

Function questions often look like simple syntax recall, but one prototype, copied pointer, storage-duration rule or pair of parentheses can change the answer. Pair each rule with a cue and verify it against an exact trace. Keep the central idea clear: a call creates fresh parameter objects, every C argument is passed by value, and apparent caller mutation happens only when a copied pointer reaches caller-owned storage.

1. Function anatomy: declaration, definition and call

Part

Pattern

When to use

Declaration

R name(T1 p1, T2 p2);

Place it before a call when the definition is not already visible.

Definition

R name(T1 p1, T2 p2) { /* body */ }

Supply the body.

Call

result = name(a1, a2);

Initialise fresh parameters and transfer control.

int add(int, int); matches int add(int a, int b) { return a + b; }, so add(7, 5) returns 7 + 5 = 12. Parameter names are optional and are not part of the function type. A later definition double add(int a, int b) conflicts with the declared int return type.

int ready(void); explicitly means no parameters. In conventional C17-style questions, int ready(); leaves them unspecified; if C23 is named, apply that version. C has no overloading or default arguments. A function cannot return an array or function type, but it may return a suitably declared pointer to one.

2. Parameters and arguments: value copies, pointers and arrays

Formula: on f(expr), evaluate and convert expr, then initialise a new parameter object. Use when: deciding whether a caller's scalar changes.

Consider:

c
int adjust(int x, int *y) {
    x += 2;
    *y += x;
    return x + *y;
}

For int a = 3, b = 4; int r = adjust(a, &b);, entry is x = 3, y = &b. Then x += 2 makes x = 5, while a stays 3. Next, *y += x changes b from 4 to 9. Return: 5 + 9 = 14. Final state: a = 3, b = 9, r = 14. This is call by value because y holds a copied address.

Four-panel diagram shows caller a = 3, b = 4, callee x changing 3 to 5 and copied y pointing to b, then b = 9 and r = 14 after return.

Parameter forms int a[] and int a[10] adjust to int *a. Use when: tracing arrays or applying sizeof.

c
void bump(int a[], size_t n) {
    if (n > 1) a[1] += 3;
}

With int v[3] = {2, 5, 8}; bump(v, 3);, element two becomes 5 + 3 = 8, giving {2, 8, 8}. Inside bump, sizeof a measures the pointer, not the complete array, so pass the count separately. See the longer traces in Functions in C Programming: Complete Guide with Worked Examples.

3. Return rules and evaluation-order traps

In a non-void function, return expression; converts the expression to the return type. A void function can use return; or fall through. Closing main acts like return 0;. Use when: checking every path in another value-returning function.

c
int clamp(int x, int lo, int hi) {
    if (x < lo) return lo;
    if (x > hi) return hi;
    return x;
}

Thus clamp(17, 0, 10) = 10, clamp(-3, 0, 10) = 0, and clamp(7, 0, 10) = 7. Early returns map each condition to one explicit result.

Returning &local for an automatic local leaves a dangling pointer. Judge every returned pointer by whether its target remains alive. C also gives no left-to-right argument-evaluation promise, so do not assume left() precedes right() in combine(left(), right()). f(i++, i++) modifies i without sequencing, giving undefined behaviour. Do not invent an output.

4. Scope, storage duration and linkage

Declaration

Scope and duration or linkage

When to use

Ordinary block local

Block scope, automatic duration; fresh object per call

Reset it when tracing each call.

Block-local static

Block scope, static duration; value persists

Carry its value into the next call.

File-scope static function

Internal linkage

Keep the name inside one translation unit.

Ordinary file-scope function declaration

Normally external linkage

Allow a matching definition in another translation unit.

extern int total; is a declaration rather than a definition, so it does not itself provide the object's definition. If total has external linkage, its definition may appear later in the same translation unit or in another translation unit. With no prior visible declaration, this extern declaration gives total external linkage; after a visible file-scope static int total;, however, it refers to that internally linked name. For shadowing, choose the nearest declaration without confusing scope, lifetime and linkage.

For int next(void) { static int c = 2; int t = 10; c += 3; t += c; return t; }:

  • Call 1: c: 2 -> 5, fresh t: 10 -> 15, return 15.

  • Call 2: c: 5 -> 8, fresh t: 10 -> 18, return 18.

  • Call 3: c: 8 -> 11, fresh t: 10 -> 21, return 21.

Output is 15 18 21: static c persists; automatic t resets.

5. Recursion: base case, recurrence and stack cost

Every recursive function needs a base case and a progress step. For one constant-time call reducing size d to d - 1:

T(d) = T(d - 1) + Theta(1) = Theta(d), with stack space Theta(d).

Use when: the argument moves one step towards the base case on each call.

For digit_sum(472), where the function returns n below 10 and otherwise returns n % 10 + digit_sum(n / 10):

  1. digit_sum(472) = 2 + digit_sum(47).

  2. digit_sum(47) = 7 + digit_sum(4).

  3. digit_sum(4) = 4, the base case.

  4. Unwind: 7 + 4 = 11, then 2 + 11 = 13.

There are three active frames at maximum. For a positive d-digit input, time and stack space are both Theta(d).

Recursion stack diagram for digit_sum(472) shows frames 472, 47 and 4, returns 4, 11 and 13, with three active frames and linear time and stack.

A missing base case exhausts resources. A missing return can discard the result. A static local is shared across invocations, not copied per frame. Recursion alone does not improve asymptotic time.

6. Function pointers and callbacks

int (*op)(int, int); is a pointer to a function returning int; int *op(int, int); is a function returning int *. Use when: reading outwards from the identifier while respecting parentheses. In ordinary assignment and call contexts, a function name converts to its function pointer.

Let add return a + b, mul return a * b, and apply(op, x, y) return op(x, y). Then apply(add, 4, 6) = 4 + 6 = 10, while apply(mul, 4, 6) = 4 * 6 = 24. Only the operation changes.

A dispatch table int (*ops[2])(int, int) = {add, mul}; gives ops[0](4, 6) = 10 and ops[1](4, 6) = 24. The pointed-to function type must match the declared return and parameter types. Do not use an object pointer such as void * as a portable substitute for a function pointer.

7. Function-question pre-solve scan

Typical tasks include selecting a compatible prototype, tracing copies or pointer mutation, carrying a static local, unwinding recursion, parsing a function pointer, and separating type conflicts, missing definitions and undefined behaviour.

Before solving:

  1. Mark the C version if stated.

  2. Copy the visible prototype.

  3. Box every parameter type.

  4. Write “value copied” above each argument.

  5. Draw arrows only for valid pointers.

  6. Mark automatic and static locals.

  7. Identify the recursion base case.

  8. Refuse to assign a fixed output to undefined behaviour or unspecified argument order.

Use Functions in C Mistakes: Fix 11 Errors in Calls, Parameters and Recursion for an error-focused audit; this section keeps the pre-solve scan compact.

8. Sixty-second functions revision card and next step

  • Prototype before an unseen call.

  • Parameter names do not determine compatibility.

  • f(void) means no parameters.

  • Arguments initialise fresh parameter objects.

  • C uses call by value.

  • A copied pointer can mutate its pointee.

  • Array parameters adjust to pointers in the basic forms above.

  • Automatic locals reset; static locals persist.

  • Recursion needs a base case and progress.

  • (*op)(...) is the function-pointer form.

Final checks: verify every non-void path returns a value, every returned pointer outlives the call, and every function-pointer declarator has the required parentheses.

For a broader path through concepts, MCQs and coding questions, continue with the C Language course. For structured exam preparation beyond this topic, use C Fundamentals Formula Sheet for GATE CS: Rules, Traces and Revision Notes, which covers the wider C syllabus. The function revision centres on calls, scope, recursion and callbacks. Or browse the Coding & DSA Courses. The short version is simple: copy the call state, follow lifetime and indirection, then calculate.