Functions
A function is a named piece of code that can be called from anywhere in the program: a command of your own. It can be given values to work with, its parameters, and give back a result with Return.
Function Area#(width#, height#) Return width * heightEnd FunctionFunction ShowScore(points) Text 10, 10, "Score: " + pointsEnd FunctionPrint Area(3, 4.5) ; 13.5ShowScore 1200
Calling a function
When its result is used, brackets go round the values given to a function: a# = Area(3, 4). When it is called on its own, as a statement, the brackets can be left out: ShowScore 1200 and ShowScore(1200) do the same. A function can be written anywhere in the program, before or after the places that call it, and can call itself:
Function Factorial(n) If n <= 1 Then Return 1 Return n * Factorial(n - 1)End Function
The result
The tag on a function's name sets what it gives back, as for a variable: none or % an integer, # a float, $ a string, or .TypeName an object. Return leaves the function, giving back its value; a function that reaches End Function without one gives back 0, 0.0, an empty string or Null.
Parameters
Parameters are like variables made fresh for each call, filled with the values given. They take type tags too. Giving a parameter a default value makes it optional:
Function Greet(name$, times = 1) For i = 1 To times Print "Hello, " + name$ NextEnd FunctionGreet "Ann" ; onceGreet "Bob", 3 ; three times
Numbers and strings are copied in, so changing a parameter inside the function doesn't change the variable it came from. An object passed to a function is the same object, though, so changes to its fields are seen everywhere.
Variables inside functions
Variables first used inside a function belong to that call of it, and disappear when it returns. To use the same variable as the main program, declare it Global; see Variables. Arrays made with Dim and constants can be used inside any function.
