Home/Notes/Functions & Recursion
Board Exam Notes

Functions & Recursion Notes

Questions

3 questions per paper

Difficulty

Medium

Importance

High yield for university theory and practical exams

Overview

Functions and recursion are the building blocks of modular programming in C, allowing complex problems to be broken down into manageable, reusable segments. Mastering these concepts is essential for writing clean, efficient code and is a primary focus area for university programming exams and technical interviews.

Function Declaration and Call

Functions allow code to be organized into logical units that perform specific tasks, enhancing readability and maintainability. In university exams, you must correctly distinguish between the prototype, definition, and the actual invocation.

  • Declaration consists of return type, function name, and parameter list ending with a semicolon.
  • Actual arguments are used during the function call, while formal parameters are used in the function definition.
  • Pass by value copies the actual parameter data, whereas pass by reference/pointer modifies the original variable.
  • The return statement terminates function execution and passes control back to the caller.

Recursion

Recursion occurs when a function calls itself, relying on a base case to stop execution and prevent infinite loop errors. Understanding the call stack mechanism is crucial for tracing recursive logic in exam problems.

  • Every recursive function must have a base case to prevent stack overflow.
  • Recursive calls operate on the LIFO (Last In, First Out) principle using the system stack.
  • Recursion typically incurs more memory overhead compared to iterative loops.
  • The time complexity of recursive functions is often solved using recurrence relations.

Storage Classes

Storage classes define the scope, visibility, and lifetime of variables in a C program. Examiners frequently test your knowledge of where variables are stored in memory and how long they persist.

  • auto: Default storage class for local variables, stored in the CPU stack.
  • static: Maintains its value between function calls and has a local scope.
  • extern: Used to declare global variables defined in other files.
  • register: Hints the compiler to store variables in CPU registers for faster access.

Exam Tip

Always trace the stack frames for recursive functions on your scratchpad to avoid off-by-one errors in loop termination.

Common Mistakes

  • Forgetting the base case in a recursive function leading to stack overflow.
  • Confusing the scope of 'static' variables with global variables.
  • Passing large structures by value instead of by pointer, leading to inefficient memory usage.

More Revision Notes

Ready to test yourself?

Play topic-wise Functions & Recursion questions in Aspirant Arcade — gamified MCQ practice.

Download Free