text
stringlengths
0
897
print "No Such Thing" - "Item"
```
Line 1 prints out "How", which is what's left after you subtract "dy" from "Howdy". The second line similarly lops ".png" off the end of "Alien.png", and leaves you with just "Alien" (hey, that example's actually useful!). And the third example prints "No Such Thing" -- the subtraction has no effect in this case, sin...
Multiplication with numbers can be thought of as a shortcut for repeated addition. For example, `5 * 3` is the same as `5 + 5 + 5`. With strings, it's same idea: you can multiply a string by a number, and this is the same as adding the string that many times. So `"Foo" * 3` is the same as `"Foo" + "Foo" + "Foo".` W...
{i:"string, replication"}
{caption: "Spam and beans"}
```miniscript
print "Spam, " * 4 + "Baked Beans, and Spam!"
```
Multiplying a string by a number this way is known as *string replication*. And it works not only for whole numbers. You can multiply a string by 0.5 to cut it in half, or by 2.5 to add it to itself two whole times and half of once more. Moreover, you can also use the division operator, `/`, to divide a string down ...
{pageBreak}
D> A common application of string replication is to print out some number of characters in a row. For example, `print "=" * 60` is much easier way to print a row of 60 equal signs than counting them as you type them in. And sometimes you'll need to print out some number of spaces that is calculated based on the lengt...
Here are the operators you can use with strings:
{i:"string, operators;operators, string"}
| `+` | concatenation |
| `-` | subtraction |
| `*` | replication (by a number) |
| `/` | division (by a number) |
All of these operators begin with a string on the left side of the operator. With `*` and `/`, the right-hand side must be a number. But with `+` and `-`, the right-hand side can be anything... but if it's not a string, it gets automatically converted to a string, before being added to or subtracted from the left-han...
This is frequently used with `print`, to output some combination of strings and numeric values.
{caption: "The number 3.5E6 is automatically converted to a string"}
```miniscript
print "Your projected net worth is $" + 3.5E6 + "!"
```
## Functions
MiniScript has a variety of built-in *functions*, which are like commands or operations that you invoke by name. You've used one a bunch of times already: `print`.
function
: a block of code that can be called upon from other code
When you write
```miniscript
print 6 * 7
```
You are really saying, "Invoke the function called *print* and give it the value of 6 \* 7." (The values you give a function are called its *arguments* in computer jargon.)
argument
: a value given as input to a function
You probably saw this concept in middle-school math. Whenever you wrote an equation of like *y = f(x)*, this meant there was some function *f* that took *x* as its argument, and returned a value equal to *y*.
(You may also hear the term *parameter* -- for the sake of Day 2, consider this a synonym for *argument*. We'll elaborate on this in Chapter 11.)
Functions in MiniScript can cause things to happen -- for example, `print` causes output to appear on the screen -- or they can return a result (or both). Many of the built-in functions in MiniScript are the sort that return a result, usually based on the argument (or several arguments) that you give it. For example,...
```miniscript
print sign(-0.1)
```
Try some other values, too.
D> Experimenting with code is an extremely powerful way to learn. Don't give up your power -- experiment!
Notice that when we're supplying arguments to a function, and doing something with the result, then we need to put parentheses around the arguments, just like you did with *f(x)* in school. Math functions are usually like this; try the `sqrt(x)` function (which returns the square root of its argument) for another exam...
What if you need to supply more than one argument? In this case you just list all your arguments, separated by commas, within the parentheses. For example, there is a `round(x,d)` function that rounds a number *x* to *d* decimal places. You invoke it like this:
{caption:"Rounding numbers"}
```miniscript
print round(1.23468, 0)
print round(1.23468, 1)
print round(1.23468, 2)
print round(1.23468, 3)
print round(1.23468, 4)
```
In the first line above, the first argument is `1.23468`, and the second argument is `0`. Order matters -- that's the only way MiniScript can know which argument is which. So when you use an unfamiliar function, you'll look up exactly what the arguments are so you can supply them in the correct order.
Some functions have *optional* arguments. These can be left out, and MiniScript will assume some value for them. Because order matters, you can only omit arguments at the *end* of the argument list; you can't omit something in the middle and then provide more arguments after that, because the computer would have no w...
optional argument
: an argument that may be omitted when calling a function; a default value is used in this case. Also called an *optional parameter*.
As an example, let's revisit that `round` function above. The second argument -- the number of decimal places to round to -- is actually optional, with a default value of 0. So if you write just `round(1.23468)`, with a single argument, this is exactly the same as writing `round(1.23468, 0)`. But don't take my word ...
However, sometimes a function doesn't need *any* arguments. Two good examples are `pi` and `rnd`. The `pi` function always returns the value of pi, `3.141593`. Conversely, `rnd` (which is short for "random") returns a different random number between 0 and 1 every time you call it. Try it:
{caption:"Using `pi` and `rnd`"}
```miniscript
print pi
print rnd
print rnd
print rnd
```
You should run this script multiple times, until you're satisfied that `rnd` really does return a different result every time you use it.