text stringlengths 0 897 |
|---|
Now inside a function, when you do an assignment, like |
``` |
test = function() |
y = "abc" |
end function |
``` |
it is actually stored as the value for "y" in this local variables map. That of course does not affect the globals map at all, nor does it affect the locals in any other function call. And when the function exits — either via a `return` statement or by reaching the `end function` line — the local variables map is sim... |
So that's what happens when you assign to a variable: at the global scope, it stores the value in `globals`; and inside a function, it stores it in the local variables for that function call. What about when you *use* the value of a variable? |
In that case, MiniScript first looks for that variable in the local variables map. If it finds it, it returns that value and is done. But if it doesn't find it, then it looks at the global variables. And if it finds a matching variable, it returns you that value instead. So global variables can always be accessed f... |
This is super important, and probably not completely clear if it's the first time you've encountered the concept. Here's an example that will help. Type this in carefully: |
```miniscript |
x = 0 // assign to x as a GLOBAL variable |
test = function() |
print "Inside test: x is " + x // read the GLOBAL variable x |
x = 123 // assign to x as a LOCAL variable |
print "Still inside test: x is now " + x // read LOCAL x |
end function |
print "Here at the global scope, x is " + x // read GLOBAL x |
test |
print "Back at global scope again, x is " + x // read GLOBAL x |
``` |
When you run this, the output is: |
```terminal |
Here at the global scope, x is 0 |
Inside test: x is 0 |
Still inside test: x is now 123 |
Back at global scope again, x is 0 |
``` |
Let's walk through it step by step. Line 1 is code at the global scope, and it assigns to `x`, so that creates a global variable (actually the same as doing `globals["x"] = 0`). Lines 2-6 define the function (and store it in another global variable called `test`). |
Line 8 runs next, and since it's at the global scope, it prints out the value of global variable `x`, which is 0. Line 9 calls the `test` function. So line 3 runs, and prints a variable called `x`... since there isn't a local variable with that name at this point, it prints out the value of the global variable again. |
Line 4 then assigns 123 to `x`. This is inside a function, so it stores the value 123 in the local variables map under "x". And on line 5, when we print the value of `x` again, it now finds this local variable, and prints out that value (123). Notice that this assignment does nothing at all to the globals map. |
Then the function ends, and we pop back to where it was called. Here on line 10, back at the global scope, we print out the value of `x` again, which prints out the global value — still 0. |
D> Almost all programming languages have a very similar concept of local and global variables, though they may differ in the details — in Lua, for example, assignment always creates a global variable by default, even inside a function. |
If that still seems confusing, don't let it worry you too much. It will sink in as you use it. As your programs get more complex, you'll discover that local variables are a wonderful invention; they give you a fresh sheet of scratch paper for every small task (function) you need to do, and then recycle that sheet com... |
## Building a Program, Piece by Piece |
Let's end the chapter with one more example — a shortish program that uses several functions. But this time we're going to go through how you would actually write and test such a program. |
The task for this program is to convert normal text to Pig Latin — a word game in which the initial part of a word, up to the first vowel, is stripped off and then appended to the end of the word, followed by "ay". Or for words that begin with a vowel, you just append "yay" to the end. |
Thinking about how to break this complex task down would go something like this: |
- Well, we want to operate on each word separately. |
- For a single word, we'll need to split it into the part before the first vowel, and everything after that point. |
- That requires knowing if a character is a vowel or not. |
The rest is pretty easy: just swapping the two parts and adding "ay" or "yay". So now we have a plan; time to start coding! And often the best way to begin is with the smallest, simplest function. In this case, that would be a function to tell if a character is a vowel or not. So we write that, and include some tes... |
{caption:Step 1 of our Pig Latin program} |
```miniscript |
// return true if c is a vowel, false if not |
isVowel = function(c) |
return "aeiou".indexOf(c) != null |
end function |
print isVowel("x") |
print isVowel("a") |
``` |
We begin by writing a comment describing what we want to do. Then we make a function, `isVowel`, that takes one character (`c`) as its parameter. It uses `indexOf` to see whether that character is in the set of vowels. `indexOf` returns `null` if the thing you're looking for is not found in the string you're searchi... |
(Type that in and verify that it works before moving on.) |
OK, that's a handy function that MiniScript didn't have before; now we have it. Let's use it for the next bit: something to split a word into the part before the first vowel, and the part after. Keep what you have above, and add this: |
{caption:Step 2 of our Pig Latin program} |
```miniscript |
// split a word into the part before the first vowel, |
// and the part from the first vowel to the end |
splitWord = function(word) |
for i in word.indexes |
if isVowel(word[i]) then return [word[:i], word[i:]] |
end for |
return [word, ""] // no vowel found! |
end function |
print splitWord("three") |
print splitWord("omlets") |
``` |
Again, we begin with a comment. That's always a good idea; it helps you focus on exactly what the function you're about to write should do, and when you come back to this code later, it reminds you what it does. |
Then we iterate over all the indexes in the given `word`. If the word is five characters long, then `word.indexes` will be `[0, 1, 2, 3, 4]`. The `if` statement uses our handy `isVowel` function to determine if character `i` of the word is a vowel; if it is, then we return a list containing the part of the word befor... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.