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

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

    Chapter Roadmap: Python Functions and Recursion

    Chapter Roadmap: Python Functions and Recursion

    1
    Recursive Function Tracing and Return Values
    Core Focus. Execution flow, stack frames, return propagation.
    2
    Chapter Mastery Achieved

    The Mental Model: Stack Frames as Isolated Worlds

    The Mental Model: Stack Frames as Isolated Worlds

    Recursion is not a loop that reuses memory. It is a chain of independent function invocations.

    Frame 3: ans = 1
    Frame 2: ans = 1
    Frame 1: ans = 1
    • Each call New Stack Frame (new local namespace)
    • Parent state is frozen at the call site
    • Execution resumes only after child returns a value
    • Variables like n, ans, or i are distinct copies per frame
    Key Insight: When tracing, never update a variable "globally" across recursive calls unless explicitly passed by reference (which Python primitives do not support). Each ans = 1 in a new call starts fresh.

    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.

    Tree Summation Pattern

    Tree Summation Pattern

    Problem Structure

    def count(child_dict, i):
        if i not in child_dict.keys():
            return 1
        ans = 1
        for j in child_dict[i]:
            ans += count(child_dict, j)
        return ans

    Trace Execution

    FrameArgsLocal ansChildrenReturns
    (dict, 0)init 1[1,2]
    (dict, 1)init 1[3,4,5]
    (dict, 2)init 1[6,7,8]
    leaves—none1 each
    Critical Observation: ans = 1 executes fresh in every frame. There is no shared accumulator. The summation happens via return value aggregation, not mutable state.

    More notes in this unit

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

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

    Free preview ends here

    Login to view the complete 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.