Circle of Learning Logo CIRCLE OF LEARNING Excellence in Education
CBSE Class 12 · Computer Science Chapter 2 Session 2026-27

Functions and Recursion in Python Formulas & Cheat Sheet

Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.

Core Concepts & Theoretical Foundations

  • Function Definition & Anatomy: Defined using `def fname(params):`. Consists of header, parameter list, docstring, body statements, and optional `return` statement (defaults to returning `None`).
  • Types of Arguments: 1) Positional arguments (matched by order), 2) Default arguments (default values assigned right-to-left in signature), 3) Keyword arguments (passed by parameter name, order irrelevant).
  • Scope of Variables & LEGB Rule: Python resolves variable names in strict hierarchy: Local (inside current function) -> Enclosing (nested outer function) -> Global (module level) -> Built-in (Python built-in namespace).
  • `global` Keyword: Used inside a function to modify a variable declared at the module level.
  • Recursion: A programming technique where a function calls itself directly or indirectly to solve a smaller instance of the same problem. Must have: 1) Base case (terminating condition), 2) Recursive case (progressing toward base case).

Chapter Formulas & Equations (10 entries)

# Topic / Formula Name Mathematical Form & Equation Conditions, Meaning & Board Tips
1 LEGB Scope Resolution Hierarchy
Scope Order:L→E→G→B\text{Scope Order}: L \to E \to G \to B

Python searches innermost local scope first and broadens outwards until matching name is found or NameError raised.

2 Default Parameter Right-to-Left Rule
Valid:f(pos,pos,def=v);SyntaxError:f(def=v,pos)\text{Valid}: f(\text{pos}, \text{pos}, \text{def}=v); \quad \text{SyntaxError}: f(\text{def}=v, \text{pos})

All positional arguments without defaults must precede parameters with default values.

3 Recursive Factorial Mathematical Function
n!={1n≤1n×(n−1)!n>1n! = \begin{cases} 1 & n \le 1 \\ n \times (n - 1)! & n > 1 \end{cases}

Classic recursion with base case n <= 1 and O(n) call stack depth.

4 Recursive Fibonacci Mathematical Function
Fn={nn∈{0,1}Fn−1+Fn−2n≥2F_n = \begin{cases} n & n \in \{0, 1\} \\ F_{n-1} + F_{n-2} & n \ge 2 \end{cases}

Branching tree recursion with exponential O(2^n) time complexity unless memoized.

5 Recursive Binary Search Time Complexity
T(n)=O(log⁡2n)T(n) = O(\log_2 n)

Halves search interval at each recursive call on sorted arrays.

6 Pass-by-Object-Reference Mechanism
ΔLmutable  ⟹  Reflected Globally;Δximmutable  ⟹  Local Rebinding\Delta L_{\text{mutable}} \implies \text{Reflected Globally}; \quad \Delta x_{\text{immutable}} \implies \text{Local Rebinding}

Core memory model explaining argument passing in Python.

7 Maximum Recursion Depth Limit
Depthmax⁡≈1000  ⟹  RecursionError: maximum recursion depth exceeded\text{Depth}_{\max} \approx 1000 \implies \text{RecursionError: maximum recursion depth exceeded}

Protects C call stack from buffer overflow when base case is missing.

8 Multiple Return Values Packing
return a,b  ⟹  (a,b)\text{return } a, b \implies (a, b)

Enables returning multiple results cleanly without custom data wrappers.

9 Global Scope Variable Rebinding Rule
global x  ⟹  x←Global Namespace\text{global } x \implies x \leftarrow \text{Global Namespace}

Without `global`, assigning `x = 10` inside a function creates a new local variable shadowing the global.

10 Sum of Digits Recursive Identity
S(n)=(n(mod10))+S(n//10)S(n) = (n \pmod{10}) + S(n // 10)

Standard board exam recursive algorithm breaking integer into decimal units.

Step-by-Step Worked Board Exam Examples

Example 1: Trace the output of the following Python function: def func(x, y=5): x = x + y y = x - y print(x, y, end=' ') return x x = 10 y = 20 x = func(x, y) print(x, y)

1. Global variables: x = 10, y = 20

2. Function call: func(10, 20) with positional arguments x=10, y=20 (default y=5 is overridden)

3. Inside func:

x = 10 + 20 = 30

y = 30 - 20 = 10

print(x, y, end=' ') prints '30 10 '

returns x = 30

4. Outside func:

Global x is assigned return value: x = 30

Global y remains untouched: y = 20

print(x, y) prints '30 20'.

Ans: Output: 30 10 30 20

Example 2: Write a recursive Python function `power(a, b)` to calculate a raised to the power b (a^b) without using the `**` operator or loops.

1. Base case: Any number raised to power 0 is 1. If b == 0, return 1.

2. Recursive step: a^b = a × a^(b - 1). Return a * power(a, b - 1).

3. Python code:

def power(a, b):

if b == 0:

return 1

return a * power(a, b - 1)

Ans: ```python def power(a, b): if b == 0: return 1 return a * power(a, b - 1) ```

Common Mistakes to Avoid in Exam

  • Placing default arguments before non-default arguments: `def func(a=10, b):` causes `SyntaxError: non-default argument follows default argument`.
  • Forgetting the `global` declaration when modifying global variables: Writing `x += 1` without `global x` causes `UnboundLocalError: local variable 'x' referenced before assignment`.
  • Omitting the base case in a recursive function: Causes infinite recursion ending in `RecursionError: maximum recursion depth exceeded in comparison`.
  • Mutating default arguments: Using mutable default arguments (e.g. `def append_to(item, target=[]):`) shares the same list across subsequent calls. Use `target=None` instead.

Target Practice Questions

Q1. Predict output: `def test(a, b=2, c=3): return a + b * c; print(test(1, c=4))`

Correct Answer: Parameters: a=1, b=2 (default), c=4 (keyword). Result: 1 + (2 * 4) = 1 + 8 = 9.

Q2. State whether Python uses Call by Value or Call by Reference.

Correct Answer: Python uses 'Call by Object Reference' (or Call by Sharing). Object references are passed by value.

Q3. What is the time complexity of the classic recursive Fibonacci algorithm?

Correct Answer: O(2^n) exponential time complexity due to redundant recomputations of overlapping subproblems.

Frequently Asked Questions

Q. What is the difference between formal parameters and actual arguments?

Formal parameters are variables specified in the function header definition (e.g. `def add(a, b):`). Actual arguments are the values, expressions, or objects passed to the function during invocation (e.g. `add(5, x + 2)`).

Q. What is the LEGB rule in Python?

It defines the name resolution order in Python: Local scope, then Enclosing (nested outer function) scope, then Global (module) scope, and finally Built-in namespace.

Q. What happens if a function does not have a return statement?

In Python, every function returns a value. If no explicit `return` statement is encountered, the function automatically returns `None`.

Q. What are the two mandatory components of every recursive function?

1) A Base Case (a terminating condition that stops recursion without calling itself), and 2) A Recursive Case (the function calling itself on a reduced/smaller subproblem).

Q. What is the Call Stack in recursion?

The Call Stack is the runtime stack data structure managed by the operating system/Python interpreter that stores stack frames (activation records with local variables, arguments, and return addresses) for each active function invocation.

← View All 11 Chapters in Class 12 Computer Science Formula Book