1. The Call Stack & Function Execution Lifecycle
When a program invokes a function, the CPU allocates a dedicated Stack Frame on top of the call stack:
TEXTSYSTEM SCHEMATIC
High Memory
+-------------------------------------------------------------+
| Stack Frame: main() |
| - Local variables: int x = 10, int y = 20 |
+-------------------------------------------------------------+
| Stack Frame: calculateDistance(x, y) |
| - Return Address (instruction pointer in main) |
| - Parameters: a = 10, b = 20 |
| - Local variables: double result = 22.36 |
+-------------------------------------------------------------+
Low Memory (Stack grows downward)When calculateDistance() reaches its return statement, its stack frame is instantly popped from memory, and the CPU resumes execution in main() at the saved return address.
§
2. Parameter Passing: Pass-by-Value vs. Pass-by-Reference
In C++:
CPPSYSTEM SCHEMATIC
#include <iostream>
// Pass-by-Value: Creates an isolated local copy (original remains 10)
void incrementByValue(int num) {
num += 1;
}
// Pass-by-Reference (&): Directly accesses the caller's memory address
void incrementByReference(int &num) {
num += 1;
}
int main() {
int score = 10;
incrementByValue(score);
std::cout << "After pass-by-value : " << score << std::endl; // Still 10!
incrementByReference(score);
std::cout << "After pass-by-reference: " << score << std::endl; // Now 11!
return 0;
}In Python:
Python uses Pass-by-Object-Reference:
- Primitive immutable objects (integers, floats, strings, tuples) cannot be altered in-place inside functions.
- Mutable objects (lists, dictionaries, sets) can be modified in-place, affecting the caller's data directly!
PYTHONSYSTEM SCHEMATIC
def append_system_log(log_list, entry):
# Modifies the caller's original list in-place!
log_list.append(entry)
audit_logs = ["System boot OK"]
append_system_log(audit_logs, "Service daemon started")
print(audit_logs) # ['System boot OK', 'Service daemon started']§
3. Modular Architecture: Organizing Code into Modules
Professional software is never written in a single massive file. Code is decomposed into reusable modules:
TEXTSYSTEM SCHEMATIC
Project Architecture:
+-- main.py (Application entry point and user interface orchestration)
+-- geometry.py (Dedicated math and geometric calculation algorithms)
+-- storage.py (File I/O, database connections, and serialization)Example: Creating a Custom Python Module
Create geometry.py:
PYTHONSYSTEM SCHEMATIC
# geometry.py - Geometric calculation utilities
import math
def calculate_circle_area(radius: float) -> float:
"""Computes the surface area of a circle given its radius."""
if radius < 0:
raise ValueError("Radius cannot be negative.")
return math.pi * (radius ** 2)
def calculate_hypotenuse(a: float, b: float) -> float:
"""Computes the hypotenuse using the Pythagorean theorem."""
return math.sqrt(a**2 + b**2)In main.py, import and consume the module cleanly:
PYTHONSYSTEM SCHEMATIC
# main.py
import geometry
radius = 5.0
area = geometry.calculate_circle_area(radius)
print(f"Area of circle with radius {radius} = {area:.4f}")