<!-- PDF page 58 -->

Thus when **FN p(2,3)** is being evaluated, **a** has the value **10** because it has been initialised in line 10, **x** has the value **2** because it is the first argument, and **y** has the value **3** because it is the second argument. Both **x** and **y** albeit also defined in line **10** are treated as **LOCAL** variables and their value doesn't modify their global counterparts. The result is then, **10+2\*3=16**.

When **FN q()** is being evaluated, on the other hand, there are no arguments. So **a**, **x** and **y** all still refer to the variables and have values **10**, **0** and **0** respectively. The answer in this case is **10+0\*0=10**.

Now change line 20 to:

```
20 DEF FN p(x,y)=FN q()
```

This time, **FN p(2,3)** will have the value **10** because **FN q** will still go back to the variables **x** and **y** rather than using the arguments of **FN p**.

**DEF FN** can take parameters passed by **REF**erence as is obvious from the next example:

```
DEF FN ian$(REF
         jen$(),idx)=jen$(idx)
```

Some BASICs (but not *NextBASIC*) have functions called **LEFT$**, **RIGHT$**, **MID$** and **TL$**.

**LEFT$** (*a$,n*) gives the substring of *a$* consisting of the first *n* characters.

**RIGHT$** (*a$,n*) gives the substring of *a$* consisting of the characters from *nᵗʰ* on.

**MID$** (*a$, n₁, n₂*) gives the substring of *a$* consisting of *n₂* characters starting at the *n₁ᵗʰ*.

**TL$** (*a$*) gives the substring of *a$* consisting of all its characters except the first.

You can write some user-defined functions to do the same. For example:

```
10 DEF FN TL$(a$)=a$(2 TO )
20 DEF FN LEFT$(a$, n)=a$( TO
   n)
```

Check that these work with strings of length **0** or **1**.

Note that our **FN LEFT$** has two arguments, one a number and the other a string.

A function *cannot* have integer arguments, nor use integer expressions in its definitions.

### NextBASIC functions within integer expressions

We already discussed the usage of the **INT {...}** keyword which converts any floating point expression into an integer expression, but in many cases this can be slow. In other cases the values produced by a function are either plain 8 or 16 bit integers which means that integer-only versions of said function would provide significant boost over their standard counterparts. *NextBASIC* caters for these cases with special integer-only forms of the following functions:

<table>
<tbody>
<tr><td><strong>ABS</strong> <em>n</em></td><td>Return the ABSolute value of <em>n</em> – See this <em>Chapter</em></td></tr>
<tr><td><strong>IN</strong> <em>n</em></td><td>Read value from <em>Hardware Port n</em> – See <em>Chapter 22</em></td></tr>
<tr><td><strong>INPUT</strong> <em>n</em></td><td>Read/Define input controllers – See <em>Chapters 17 and 22</em></td></tr>
<tr><td><strong>REG</strong> <em>n</em></td><td>Read value from <em>Next Register n</em> – See <em>Chapter 22</em></td></tr>
<tr><td><strong>PEEK</strong> <em>a</em></td><td>Read byte from address <em>a</em> in memory – See <em>Chapter 23</em></td></tr>
</tbody>
</table>

