|
| 1 | +# Diagrams: Functions Explained |
| 2 | + |
| 3 | +[Back to concept](../functions-explained.md) |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Function Call Sequence |
| 8 | + |
| 9 | +When you call a function, Python jumps into it, does work, then comes back with a result. |
| 10 | + |
| 11 | +```mermaid |
| 12 | +sequenceDiagram |
| 13 | + participant Caller as Your Code |
| 14 | + participant Func as Function |
| 15 | +
|
| 16 | + Caller->>Func: Call: greet("Alice") |
| 17 | + Note over Func: name = "Alice" |
| 18 | + Note over Func: Run the function body |
| 19 | + Note over Func: Build result: "Hello, Alice!" |
| 20 | + Func-->>Caller: Return: "Hello, Alice!" |
| 21 | + Note over Caller: Continue with the result |
| 22 | +``` |
| 23 | + |
| 24 | +## Parameter Passing Flow |
| 25 | + |
| 26 | +Arguments are values you send IN. The return value is what comes back OUT. |
| 27 | + |
| 28 | +```mermaid |
| 29 | +flowchart LR |
| 30 | + subgraph Caller |
| 31 | + A1["area = calculate(5, 3)"] |
| 32 | + end |
| 33 | +
|
| 34 | + subgraph "Function: calculate(width, height)" |
| 35 | + B1["width = 5"] |
| 36 | + B2["height = 3"] |
| 37 | + B3["result = 5 * 3"] |
| 38 | + B4["return 15"] |
| 39 | + B1 --> B2 --> B3 --> B4 |
| 40 | + end |
| 41 | +
|
| 42 | + A1 -->|"arguments go IN<br/>5 and 3"| B1 |
| 43 | + B4 -->|"return value<br/>comes OUT: 15"| A1 |
| 44 | +
|
| 45 | + style A1 fill:#4a9eff,stroke:#2670c2,color:#fff |
| 46 | + style B4 fill:#51cf66,stroke:#27ae60,color:#fff |
| 47 | +``` |
| 48 | + |
| 49 | +## Call Stack: Nested Function Calls |
| 50 | + |
| 51 | +When functions call other functions, Python stacks them up and unwinds when each one finishes. |
| 52 | + |
| 53 | +```mermaid |
| 54 | +flowchart TD |
| 55 | + subgraph "Step 1: main() runs" |
| 56 | + S1["main() calls greet()"] |
| 57 | + end |
| 58 | +
|
| 59 | + subgraph "Step 2: greet() runs" |
| 60 | + S2["greet() calls format_name()"] |
| 61 | + end |
| 62 | +
|
| 63 | + subgraph "Step 3: format_name() runs" |
| 64 | + S3["format_name() returns "ALICE""] |
| 65 | + end |
| 66 | +
|
| 67 | + subgraph "Step 4: Unwinding" |
| 68 | + S4["greet() gets "ALICE", returns "Hello, ALICE!""] |
| 69 | + end |
| 70 | +
|
| 71 | + subgraph "Step 5: Done" |
| 72 | + S5["main() gets "Hello, ALICE!""] |
| 73 | + end |
| 74 | +
|
| 75 | + S1 --> S2 --> S3 --> S4 --> S5 |
| 76 | +``` |
| 77 | + |
| 78 | +## Scope Chain: Where Python Looks for Names |
| 79 | + |
| 80 | +When you use a variable name, Python searches in this order (LEGB rule). |
| 81 | + |
| 82 | +```mermaid |
| 83 | +flowchart TD |
| 84 | + LOOKUP["Python sees a name<br/>like x"] --> L{"1. Local?<br/>Inside this function?"} |
| 85 | + L -->|Found| DONE(["Use that value"]) |
| 86 | + L -->|Not found| E{"2. Enclosing?<br/>Inside an outer function?"} |
| 87 | + E -->|Found| DONE |
| 88 | + E -->|Not found| G{"3. Global?<br/>At the top level<br/>of the file?"} |
| 89 | + G -->|Found| DONE |
| 90 | + G -->|Not found| B{"4. Built-in?<br/>Python built-in names?<br/>(print, len, range)"} |
| 91 | + B -->|Found| DONE |
| 92 | + B -->|Not found| ERR["NameError!<br/>Python cannot find it"] |
| 93 | +
|
| 94 | + style L fill:#4a9eff,stroke:#2670c2,color:#fff |
| 95 | + style E fill:#51cf66,stroke:#27ae60,color:#fff |
| 96 | + style G fill:#ffd43b,stroke:#f59f00,color:#000 |
| 97 | + style B fill:#cc5de8,stroke:#9c36b5,color:#fff |
| 98 | + style ERR fill:#ff6b6b,stroke:#c0392b,color:#fff |
| 99 | +``` |
0 commit comments