Functions follow the same set of rules as commands, except unlike commands it's your responsibility to define the behaviour of the function. Functions are used as way of isolating a specific block of code to perform certain tasks. They are an incredibly useful component within AppGameKit and can be used to organise your code in a neat, effective and reusable manner.
A typical program will have many functions all contributing to the outcome. It could be some of these functions are responsible for creating enemies in a level, or perhaps they are used to load data that defines how the level should look, or they may be responsible for handling how the player responds to input.
This simple example shows what a function looks like and how it can be used within a program.
do myFunction ( )
sync ( ) loop
function myFunction ( ) print ( "inside a function" ) endfunction
The code within the do loop contains a call to myFunction and sync. The functionality for the sync command is handled internally by AppGameKit. myFunction is not a command within AppGameKit, so it needs to be declared within our program. To do this we need to declare our function outside of a loop and also provide information about what parameters it requires and if it returns any data to the caller. This process is handled by the function keyword. As you can see in the code listing the function keyword is followed by a name. This can be whatever you want as long as it does not clash with the name of a command or start with a number or use symbols. This is followed by an opening bracket and then a closing bracket, indicating that this particular function has no parameters. Anything following this is code associated with the function. To declare the end point of the function the keyword endfunction is used.
When you run this program it displays the string "inside a function" on screen. When myFunction is called the program execution moves within the function, executes any lines within it and then returns back to where it was called from, in this case moving into the call to the sync command.
Any kind of functionality you require can be added into a function listing. You can use loops, if statements and call other commands and functions.