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

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
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; }aftermain. 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: Putlong add(long, long);beforemainor in a header.Mistake 2, matching only name and parameter count. Why it happens:
int add(int, int);looks close. What goes wrong: It conflicts withlong add(long, long)and requires a diagnostic. Do instead: Make both agree. Thenadd(7L, 5L)returns12L, stored asresult = 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
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)startsa = 6,b = 14and endsa = 14,b = 6. Caller staysx = 6,y = 14. Do instead: Separate caller and callee boxes keep the copied values visible.Mistake 4, treating pointers as a different passing mode. Why it happens: Mutation is called pass by reference. What goes wrong: Passing
x,ytoswap_ok(int *p, int *q)mismatches types. Assigning only top,qchanges copied pointers. Do instead: Callswap_ok(&x, &y). Entry hasp = &x,q = &y,*p = 6,*q = 14. Swap leavesx = 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
Mistake 5, calculating the caller's element count with
sizeof. Why it happens:const int a[]looks like a complete array. What goes wrong: Inint sum(const int a[]), it adjusts toconst int *a. On a model withsizeof(int *) = 8andsizeof(int) = 4,sizeof a / sizeof a[0]is8 / 4 = 2, not four. For{3, 5, 7, 9}, it adds only3 + 5 = 8. Do instead: Defineint sum(const int a[], size_t n)and callsum(values, 4). Totals3,8,15,24give24. Passing length is portable;2depends on this size model.
4. Recursion mistakes 6 and 7

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, sofact(2),fact(3), andfact(4)are0. Do instead: Return multiplication's identity1whenn == 0.Mistake 7, making no progress. Why it happens: The call is copied as
fact(n). What goes wrong:fact(4)keeps creating frames withn = 4, never reachesn = 0, and has no result before resource failure. Do instead: Trace arguments. Descent is4 -> 3 -> 2 -> 1 -> 0. Unwind isfact(0) = 1,fact(1) = 1,fact(2) = 2,fact(3) = 6,fact(4) = 24.

5. Function lifetime mistakes 8 and 9
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 guaranteed17, garbage, or a crash. Do instead: Return17, accept caller storage, or use storage that outlives the call.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; }returns7,10,13across three calls, not7,7,7. Do instead: Track visibility and lifetime. Automaticint n = 4is recreated and returns7each call.
6. Function-expression mistakes 10 and 11
Mistake 10, misreading pointer parentheses. Why it happens: Declarations look similar. What goes wrong:
int (*op)(int, int)points to a function returningint;int *op(int, int)is a function returningint *. Do instead: Read outward from the identifier. Givenint subtract(int a, int b) { return a - b; }andint (*op)(int, int) = subtract;,op(11, 4)returns7.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 finaliexists. Do instead: Use full expressions.left = i++givesleft = 3, i = 4;right = i++givesright = 4, i = 5;show(left, right)receives3and4.
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.
Keep learning
C Fundamentals Mistakes: Fix 11 Data-Type, Operator and Flowchart Errors
Short C expressions can hide strict rules. Diagnose and repair 11 common errors involving initialisation, conversions, operators, increments and flowcharts.
Grammar and CFG Mistakes for GATE: 7 Traps and How to Fix Them
Seven small grammars expose the errors that make CFG answers go wrong, with reproducible checks for classification, ambiguity, FIRST sets and CNF.
C Programming Miscellaneous Mistakes: 10 Traps in Macros, Types, Memory and Files
Ten common C traps are diagnosed at the right stage, from macro expansion and declarations to allocation, streams and undefined expressions. Each mistake includes the failure mechanism and a concrete repair.
Control Flow in C: 7 Mark-Losing Mistakes and Precise Fixes
Trace seven common C control-flow mistakes step by step, see exactly where execution changes course, and learn a short routine for checking every branch and loop.





