Python Functions and Recursion Short Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Python Functions and Recursion short notes for GATE DA: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Systematic Tracing Protocol

    Systematic Tracing Protocol

    5-Step Trace Method

    1. Box Creation: Draw rectangle for each call. Header: func(args)
    2. Local Init: Write initial assignments (ans=1, i=0) inside box
    3. Call Descent: On recursive call, draw downward arrow. Pause current box.
    4. Base Return: At base case, write return value on upward arrow
    5. Resume & Compute: Substitute returned value into parent's pending expression. Continue execution in parent box.

    Notation Convention

    Use subscript to distinguish frames:

    This prevents variable collision confusion during manual trace.

    Return Value Aggregation vs Mutable State

    Return Value Aggregation vs Mutable State

    Pattern A: Return Aggregation
    ans = 1
    for child in children:
      ans += func(child)
    return ans
    • Pure, predictable, standard
    • Requires stack space proportional to depth
    Pattern B: Mutable External State
    global total
    total += 1
    for child in children:
      func(child)
    • Rarely tested directly
    • Side effects make tracing error-prone
    Exam Rule: Unless global or nonlocal is explicit, assume Pattern A. Local variables do NOT persist across recursive calls.

    The 'Shared Variable' Illusion

    The 'Shared Variable' Illusion

    Common Misconception: "Variable ans keeps accumulating across recursive calls."
    Reality Check:
    def f(n):
      x = 0          # <- NEW x created EVERY call
      if n == 0: 
        return x
      x += f(n-1)    # <- Uses CHILD'S return, not child's x
      return x

    Diagnostic Questions

    1. Is there a global / nonlocal keyword? No Variable is local
    2. Is the variable assigned before recursive call? Yes Reset per frame
    3. Does the recursive call appear on RHS of assignment? Yes Return-value pattern
    Mnemonic: "Local = Limited to this call"

    Linear vs Tree Recursion: Trace Complexity

    Linear vs Tree Recursion: Trace Complexity

    FeatureLinear RecursionTree Recursion
    Calls per frame12 or more
    Trace shapeVertical chainBranching tree
    Stack depth
    Total calls
    Examplefact(n)fib(n)
    Exam focusReturn propagationCounting nodes / stack ops
    Exam Tip: If question asks "maximum stack size", answer = depth. If asks "number of function calls", answer = total nodes. Read carefully.

    More short notes in this unit

    chapter
    Python Functions and Recursion Short Notes for GATE DA: Concepts, Formulas, Worked Examples & Practice

    Python Functions and Recursion short notes for GATE DA: 5 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Free preview ends here

    Login to view the complete short notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.