"""
Lecture 10: Multi-level Function Calls & Environment Diagrams
==============================================================

This script demonstrates how Python creates and destroys function frames
as we call functions within functions. Perfect for tracing in Python Tutor.

Key concepts:
- Each function call gets its own frame (a private memory space)
- Frames are created on the call stack when a function is called
- Frames are destroyed when the function returns
- Variables only exist within their frame (scope)
"""


def square(n):
    """
    A simple helper function that squares a number.

    When this function is called, Python creates a new frame for it.
    The parameter 'n' and variable 'result' only exist in this frame.
    """
    result = n * n
    return result


def sum_of_squares(a, b):
    """
    Calls square() twice and adds the results.

    This demonstrates nested function calls:
    - sum_of_squares gets its own frame
    - Each call to square() creates another frame on top
    - When square() returns, its frame disappears
    """
    x = square(a)  # First call: creates square frame with n=a
    y = square(b)  # Second call: creates a NEW square frame with n=b
    return x + y


# Main execution
# ==============
# This is where the fun happens. Watch the frames appear and disappear!

total = sum_of_squares(3, 4)  # Call with a=3, b=4
print(total)  # Output: 25 (because 3² + 4² = 9 + 16 = 25)
