Scripting tutorial #5: Using Functions

What are functions?

On the previous page, I mentioned that the START and END commands act like a function. What then, is a function?

Also known as "subroutines", a function is a block of code that can be called whenever it's needed by the script that contains it. In a way, these are small "programs within programs". You could also say that they are custom commands.

How do you create functions?

Most programming languages don't use a special command like START to define where their code begins. Often, they just start executing the first lines of code they see or require the user to define a specific function, usually called something like "main" or "init". That said, all languages that support functions have a way to define where they begin and end. In our case, functions begin with the DEF command. It takes one parameter, which is the name of the function, like so:

DEF MyFunction

Once you've created a function, you can use it anywhere in your script - even in sections that come before the function was defined. Running a function is known as "calling" it, so unsurprisingly enough, the command to use a user-defined function is CALL. Like DEF, it takes just one parameter - the name of the function you're calling.

CALL MyFunction

To finish defining your function, use the END command. This signals to the editor that your function is done, so the editor will continue executing the script from wherever it saw the CALL command. This is known as "returning" from a function.

What are the restrictions on using functions?

Functions are very useful, but they do have some drawbacks that you'll need to keep in mind when you use them. The first catch is that variables aren't shared between functions. This means a variable named Green in one function and a variable named Green in another are treated as entirely different variables. While this might sound like a problem, it's actually a good thing - it prevents you from accidentally corrupting the information you're storing in variables.

Some of you may also be tempted to try "nesting" functions. Nested functions are functions that are defined within an existing function. This works in some languages, but here it's likely to confuse the scripting engine. Remember that this isn't a "general purpose" programming language - it's just a glorified way to automate some things - so don't expect it to properly handle fancier things like this.

The other big gotcha with using functions is that they cannot pass values back to the function that called them through the usual means (eg, the .RESULT variable). Nor can you pass information into a function when you call it. Instead, you'll need to make use of the Stack, a special series of "pockets" that can be passed around the program.

You'll learn more about the Stack in the next tutorial. For now, try experimenting with this script:

DEF MyFunction
ALERT "I called my function"
END

START
ALERT "Lets call my function"
CALL MyFunction
END

 

On to the next tutorial page -->

<-- Back to the previous tutorial page

Project Hosted by
Download Mike\'s Sprite Editor