Skip to content

Commit 1eaa4a4

Browse files
committed
Book Updates
-add chapter 4.2
1 parent ad3347d commit 1eaa4a4

File tree

2 files changed

+106
-70
lines changed

2 files changed

+106
-70
lines changed

book/chapter4.1.md

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,6 @@
22
title: "Chapter 4.1"
33
---
44

5-
# Functions
6-
7-
If you want your program to have some sort of organization, it is best to put sections of code into functions.
8-
9-
Functions are essentially re-usable blocks of code that can be executed any moment in a program. You can give them objects to use (often called arguments) and they can give a result after execution (returned value).
10-
11-
```
12-
func my_function(argument) {
13-
bark(argument);
14-
}
15-
16-
my_function("Hello, world!");
17-
```
18-
19-
## Function Definitions
20-
21-
As complex as it may seem, creating your own functions is relatively simple. To start, you give it a name, similar to variables, followed by arguments and the code to execute.
22-
23-
```
24-
func the_name_of_a_function(argument1, argument2) {
25-
# ...
26-
}
27-
```
28-
29-
Arguments are objects you can give the function when calling it (executing it). The arguments can be used inside the function's code, where the code will be ran when calling the function.
30-
31-
```
32-
func define_x(value) {
33-
obj x = value;
34-
}
35-
```
36-
37-
Inside the function's code, you have the option to return a value from the function when it is called with the `give` keyword.
38-
39-
```
40-
func return_value(value) {
41-
give value;
42-
}
43-
44-
bark(return_value("Hello, world!")); # outputs 'Hello, world!'
45-
```
46-
47-
## Function Calls
48-
49-
After a function is defined, you may need to use it. The function's code is **never** ran until you call it.
50-
51-
To call your function, use it's name followed by parenthesis with the arguments you gave your function (if none, don't provide any arguments.)
52-
53-
```
54-
func my_function(x) {
55-
give(x + 1);
56-
}
57-
58-
obj ten_plus_one = my_function(10);
59-
```
60-
61-
During calls, arguments are positional. This means we can't give a function with 2 arguments 10 arguments. If function takes _x_ arguments, give it _x_ arguments.
62-
63-
```
64-
func no_args() {
65-
give;
66-
}
67-
68-
no_args("Secret argument"); # this line will cause an error
69-
```
70-
71-
---
72-
title: "Chapter 4.1"
73-
---
74-
755
# Chapter 4.1: Functions
766

777
If you want your program to stay organized and readable, it’s best to group related code into **functions**.

book/chapter4.2.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)