Functions in C Mistakes: Fix 11 Errors in Calls, Parameters and Recursion

Trace eleven common Functions in C errors without guessing the output. Learn what fails, why it fails, and the precise repair for each case.

KnowledgeGate Team

Exam prep & CS education

Updated 22 Nov 20265 min read
Functions in C Mistakes: Fix 11 Errors in Calls, Parameters and Recursion

Functions questions go wrong when familiar syntax makes you assume a matching declaration, shared caller storage, a known array length, a safe lifetime, or a terminating recursive call. The eleven cases below pair each failure with its cause and a repair, then trace values that a conforming C implementation can determine. why it happens, what goes wrong, and what to do instead, using exact traces. Use CS Fundamentals for Exams & Placements for adjacent C and core CS notes.

1. Function declaration mistakes 1 and 2

  1. Mistake 1, calling before a visible compatible declaration. Why it happens: A later definition seems sufficient. What goes wrong: Put long add(long a, long b) { return a + b; } after main. Without a prototype, long result = add(7L, 5L); lacks a compatible visible declaration. A modern conforming implementation must diagnose this, so invent no output. Do instead: Put long add(long, long); before main or in a header.

  2. Mistake 2, matching only name and parameter count. Why it happens: int add(int, int); looks close. What goes wrong: It conflicts with long add(long, long) and requires a diagnostic. Do instead: Make both agree. Then add(7L, 5L) returns 12L, stored as result = 12L.

For a syntax-first walkthrough with runnable examples, see Functions in C: Syntax, Parameters and Worked Examples. For syntax reset, use C Programming.

2. Function parameter mistakes 3 and 4

  1. Mistake 3, expecting scalar parameters to change caller objects. Why it happens: Parameters seem like aliases. What goes wrong: For int x = 6, y = 14; swap_bad(x, y);, swap_bad(int a, int b) starts a = 6, b = 14 and ends a = 14, b = 6. Caller stays x = 6, y = 14. Do instead: Separate caller and callee boxes keep the copied values visible.

  2. Mistake 4, treating pointers as a different passing mode. Why it happens: Mutation is called pass by reference. What goes wrong: Passing x, y to swap_ok(int *p, int *q) mismatches types. Assigning only to p, q changes copied pointers. Do instead: Call swap_ok(&x, &y). Entry has p = &x, q = &y, *p = 6, *q = 14. Swap leaves x = 14, y = 6. C copied both addresses.

For a concept-first explanation of definitions, parameters and recursion, see Functions in C: Call by Value, Recursion & Worked Examples.

3. Array-parameter mistake 5

  1. Mistake 5, calculating the caller's element count with sizeof. Why it happens: const int a[] looks like a complete array. What goes wrong: In int sum(const int a[]), it adjusts to const int *a. On a model with sizeof(int *) = 8 and sizeof(int) = 4, sizeof a / sizeof a[0] is 8 / 4 = 2, not four. For {3, 5, 7, 9}, it adds only 3 + 5 = 8. Do instead: Define int sum(const int a[], size_t n) and call sum(values, 4). Totals 3, 8, 15, 24 give 24. Passing length is portable; 2 depends on this size model.

4. Recursion mistakes 6 and 7

Factorial recursion compares a stuck fact(4) call with the correct 4-to-0 descent and unwind to 24.
  1. Mistake 6, using the wrong identity. Why it happens: The operation is ignored. What goes wrong: With if (n == 0) return 0; return n * fact(n - 1);, fact(1) = 1 x 0 = 0, so fact(2), fact(3), and fact(4) are 0. Do instead: Return multiplication's identity 1 when n == 0.

  2. Mistake 7, making no progress. Why it happens: The call is copied as fact(n). What goes wrong: fact(4) keeps creating frames with n = 4, never reaches n = 0, and has no result before resource failure. Do instead: Trace arguments. Descent is 4 -> 3 -> 2 -> 1 -> 0. Unwind is fact(0) = 1, fact(1) = 1, fact(2) = 2, fact(3) = 6, fact(4) = 24.

Two-panel factorial recursion diagram: a stuck fact(4) self-call versus a correct descent to fact(0) and unwind to 24.

5. Function lifetime mistakes 8 and 9

  1. Mistake 8, returning a dead local. Why it happens: Its address was valid during execution. What goes wrong: int *bad(void) { int score = 17; return &score; } returns an address after the object's lifetime ends. Dereferencing it is undefined behaviour, not guaranteed 17, garbage, or a crash. Do instead: Return 17, accept caller storage, or use storage that outlives the call.

  2. Mistake 9, resetting a static local mentally. Why it happens: Scope and storage duration are merged. What goes wrong: int next(void) { static int n = 4; n += 3; return n; } returns 7, 10, 13 across three calls, not 7, 7, 7. Do instead: Track visibility and lifetime. Automatic int n = 4 is recreated and returns 7 each call.

6. Function-expression mistakes 10 and 11

  1. Mistake 10, misreading pointer parentheses. Why it happens: Declarations look similar. What goes wrong: int (*op)(int, int) points to a function returning int; int *op(int, int) is a function returning int *. Do instead: Read outward from the identifier. Given int subtract(int a, int b) { return a - b; } and int (*op)(int, int) = subtract;, op(11, 4) returns 7.

  2. Mistake 11, ordering argument side effects. Why it happens: Arguments are assumed left to right. What goes wrong: In int i = 3; show(i++, i++);, the two modifications are unsequenced, so behaviour is undefined. No portable argument pair or final i exists. Do instead: Use full expressions. left = i++ gives left = 3, i = 4; right = i++ gives right = 4, i = 5; show(left, right) receives 3 and 4.

7. Functions in C mistakes: the 60-second audit and next step

Check seven things: compatible visible signature; caller objects versus parameter copies; valid pointers; array lengths; recursion's identity and progress; lifetime versus storage duration; side effects isolated before calls.

Closed-book anchors: add(7L, 5L) = 12L; swaps 6, 14 and 14, 6; array sum and fact(4) both 24; static 7, 10, 13; callback 7; arguments 3, 4, final i = 5. Continue with C Language Course: Concepts, MCQs & Coding.