PUB MyFunction
Functions
The first function in an object is where your program starts.
Public and Private
Functions in Spin can be public or private.
-
A public function can be used by other objects that include this one.
-
A private function can only be used inside the current object.
PRI MyFunction
All Spin objects must have at least one public function.
Parameters
Sometimes you may want to pass inputs to functions to make them do different things. A good example of this is a function that draws rectangles of different sizes, or one that plays different musical notes. You can do this by adding parameters in parentheses ('('
, ')'
).
You can add one parameter:
PUB Fill(color)
Or many, separated with commas:
PUB Rectangle(x, y, w, h)
Return values
Often when you call a function, you want to calculate a value or obtain a result from it. When a function is called, it will return a value to its calling function.
variable := MyFunction
In Spin, there are two ways to specify a result from a function.
Using return
At any time in a function, you may call return
with a value.
PUB MyFunction return 234
This will cause the function to return immediately.
If no value is passed to return
, 0
is returned.
Using result
result
is a special variable that holds the value to be returned when the function exits. It defaults to 0
.
PUB MyFunction result := 234
You can alias result
with your own name by adding a :
followed by the new name to the function definition.
PUB MyFunction : myresult myresult := 234
Local variables
You may want to add variables for your function to use that aren’t visible to the rest of the program. You can add some by typing |
followed by a list of variables to declare. These will be long-sized values.
PUB MyFunction | var1, var2, var3
They are not initialized, so if you need them to be, you have to do it yourself.
PUB MyFunction | var1, var2, var3 var1 := 0 var2 := 0 var3 := 0
If you want to add both a result alias and local variables, you must add the result alias first.
PUB MyFunction : myresult | myvar myresult := 234