|
| 1 | +--- |
| 2 | +title: "Chapter 4.2" |
| 3 | +--- |
| 4 | + |
| 5 | +# Chapter 4.2: Scope |
| 6 | + |
| 7 | +When you create variables or functions, they don’t always live *everywhere* in your program. Where something “lives” and can be accessed from is called its **scope**. |
| 8 | + |
| 9 | +You can think of scope like a neighborhood: |
| 10 | + |
| 11 | +If a variable is defined inside one neighborhood (a function), it can’t just walk into another one unless it’s allowed. |
| 12 | + |
| 13 | +``` |
| 14 | +func say_hi() { |
| 15 | + obj message = "Hello!"; |
| 16 | + bark(message); |
| 17 | +} |
| 18 | +
|
| 19 | +say_hi(); |
| 20 | +bark(message); # message doesn’t exist here |
| 21 | +``` |
| 22 | + |
| 23 | +Here, the variable `message` was created inside the function `say_hi`. That means it only exists within that function and nowhere else. |
| 24 | + |
| 25 | +## Local Scope |
| 26 | + |
| 27 | +Variables declared **inside** a function are called **local variables**. |
| 28 | + |
| 29 | +They live only as long as the function is running. |
| 30 | + |
| 31 | +``` |
| 32 | +func make_cookie() { |
| 33 | + obj dough = "Sweet dough"; |
| 34 | + bark(dough); |
| 35 | +} |
| 36 | +
|
| 37 | +make_cookie(); |
| 38 | +bark(dough); # dough is gone, only existed inside make_cookie |
| 39 | +``` |
| 40 | + |
| 41 | +Local scope keeps your code safe from accidental variable name collisions. Two different functions can both have a variable named `x`, and they won’t mess each other up. |
| 42 | + |
| 43 | +``` |
| 44 | +func one() { |
| 45 | + obj x = 1; |
| 46 | + bark(x); |
| 47 | +} |
| 48 | +
|
| 49 | +func two() { |
| 50 | + obj x = 2; |
| 51 | + bark(x); |
| 52 | +} |
| 53 | +
|
| 54 | +one(); # outputs 1 |
| 55 | +two(); # outputs 2 |
| 56 | +``` |
| 57 | + |
| 58 | +## Global Scope |
| 59 | + |
| 60 | +If you create a variable outside of any function, it’s considered **global**. That means every function can access it (unless it gets shadowed). |
| 61 | + |
| 62 | +``` |
| 63 | +obj mood = "Happy"; |
| 64 | +
|
| 65 | +func show_mood() { |
| 66 | + bark(mood); |
| 67 | +} |
| 68 | +
|
| 69 | +show_mood(); # outputs "Happy" |
| 70 | +``` |
| 71 | + |
| 72 | +If you redefine a variable with the same name **inside** a function, |
| 73 | +the inner one hides (or **shadows**) the global one, modifying it: |
| 74 | + |
| 75 | +``` |
| 76 | +obj mood = "Happy"; |
| 77 | +
|
| 78 | +func show_mood() { |
| 79 | + mood = "Angry"; |
| 80 | + bark(mood); |
| 81 | +} |
| 82 | +
|
| 83 | +show_mood(); # prints "Angry" |
| 84 | +bark(mood); # prints "Angry" |
| 85 | +``` |
| 86 | + |
| 87 | +## Nested Scope |
| 88 | + |
| 89 | +Functions can live inside other functions, inner functions can see variables from their outer ones, but not the other way around. |
| 90 | + |
| 91 | +``` |
| 92 | +func outer() { |
| 93 | + obj outer_value = "Outside"; |
| 94 | +
|
| 95 | + func inner() { |
| 96 | + bark(outer_value); # ✅ can see outer_value |
| 97 | + } |
| 98 | +
|
| 99 | + inner(); |
| 100 | +} |
| 101 | +
|
| 102 | +outer(); |
| 103 | +bark(outer_value); # not visible here |
| 104 | +``` |
| 105 | + |
| 106 | +This is called **lexical scope**, meaning a function’s visibility is determined by where it was written, not where it’s called. |
0 commit comments