text stringlengths 0 897 |
|---|
print "Tip calculator!" |
print "$10 meal: $" + tip(10) + " tip" |
print "$30 meal: $" + tip(30) + " tip" |
cost = val(input("Cost of your meal? ")) |
print "You should leave $" + tip(cost) |
``` |
Run this code a few times, trying different amounts. |
D> Remember that you now know at least three ways to run MiniScript code: in the Try-It! page on the web; typed directly into the command-line REPL; and typed into a text file, which you then run with command-line MiniScript. For this example, use whichever you like best! |
A function definition begins with `function` and ends with `end function` — similar to the `if`, `for`, and `while` blocks you already know. After the `function` keyword must be parentheses, and then the name of any *parameters* you want to pass in to the function. In the example above, there is one parameter, `costO... |
parameter |
: a variable included as part of a function definition to hold a value passed in as argument |
argument |
: a value given as input to a function |
{i:"data type, function"} |
The complete function — everything from `function` to `end function` — is a value that normally gets assigned to a variable, which is `tip` in this example. Functions are the fifth data type in MiniScript (after numbers, strings, lists, and maps). If we didn't assign the newly created function to something, we would ... |
Between the `function` line and the `end function` line is any amount of MiniScript code, known as the *function body*. That code will run every time the function is called. In the example above, you probably noticed that we used the new `tip` function three times. So that function body (lines 4-6) ran three times. |
This illustrates one of the important benefits of making a function: it allows you to do the same thing at several different places in your program, without having to actually repeat the code more than once. In fact this is such a useful principle in programming, it has its own name: DRY. |
DRY (Don't Repeat Yourself) |
: The idea that a program should have no nontrivial lines of code that are repeated in more than one place. When you find the need to repeat some code, you should instead move it into a function, and then call that function wherever you need. |
{i:"`return`"} |
The other thing to notice in this example is the `return` statement. When the program reaches a `return` line, it immediately exits the function, and the value of that function (wherever it was called) is whatever value, if any, appears after `return`. So in this example, we calculate the tip amount as `x`, and then ... |
{gap:20} |
Let's look at another example. |
{caption:Simple dice roller} |
```miniscript |
rollD6 = function() |
return ceil(rnd * 6) |
end function |
die1 = rollD6 |
die2 = rollD6 |
total = die1 + die2 |
print "You rolled " + die1 + " and " + die2 |
print "for a total of " + total |
``` |
Here we defined a function called `rolld6`. It doesn't need any parameters; each time you call it, it just returns a random number between 1 and 6, like a 6-sided die. Notice that even though there are no parameters, we still need parentheses after the `function` keyword. This is the only place in MiniScript where e... |
Once the `rolld6` function has been defined on lines 1-3, we call it two times on lines 5 and 6, each time assigning the result to a variable. Then we report the value of each roll, along with the total. |
## Breaking Down Big Problems |
Programs can get pretty complicated. Recall the programs you entered in Chapter 7. They were only a few dozen lines long, but there was a lot going on, wasn't there? To understand them, you pretty much had to grasp the whole program at once. No one part of it made much sense without thinking about the rest at the s... |
Most programs in the real world are much, much longer than the ones in Chapter 7. How can anybody make sense of all that? Here's the secret: we don't understand them all at once. We break a big, complicated program down into a bunch of smaller, simpler subprograms (functions). Each function is small and simple enou... |
So a big, complicated program is really just a bunch of little functions. Indeed, if you ever find yourself writing a function that's more than about a page long, you should break it into smaller functions. Each function should do just one thing, so it's easy to understand; and it should be assigned to a variable wit... |
You'll see this principle applied in all the programs in the rest of this book. Never again will you see a long functionless code listing like those in Chapter 7. We have a major power tool now, so we're going to use it! |
## Local and Global Variables |
It's time to introduce some important terminology. |
global scope |
: code or variables in a program that are not inside any function |
local scope |
: code or variables inside a function |
Before this chapter, everything we did was at the global scope (of course, since we hadn't learned about functions yet). Code at the global scope starts executing at the top of the program, and proceeds line by line, of course honoring `if` blocks and `for`/`while` loops along the way. |
Code at the local scope, that is, inside a function, is not executed where it is defined in the program. Instead MiniScript just tucks the function away until you actually call it. You've seen this in the examples already. In the dice program, for example, the `rollD6` function was defined at the top of the program ... |
So that's one difference between local and global scope. But there's an even more important difference that applies to variables in particular. |
global variable |
: a variable assigned at the global scope |
local variable |
: a variable assigned inside a function |
Local variables can only be accessed within the function where they were assigned. They have no existence outside that function. This sounds like a limitation, but it's a really useful limitation! It means that you can use whatever variables you want inside a function, and not worry about whether some other function... |
Here's how it works: variables are actually stored in maps. At the global scope, all the variables are stored in a special map called `globals`. So when you do a simple assignment like |
``` |
x = 42 |
``` |
this is really doing |
``` |
globals["x"] = 42 |
``` |
D> Don't take my word for it -- *try it!* Open up the MiniScript REPL and experiment: enter `x = 42`, then print `globals["x"]`. Now try `globals["x"] = 123`, and print `x`. What do you find? |
Inside a function, though, things are different. Every time you make a function call, MiniScript creates a *new* map for the variables inside that call. Since functions can call other functions (or even call themselves!), this can get pretty complicated, but don't worry, it's the sort of bookkeeping computers are goo... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.