Sub |
Flow control | |
Aptilis 1 |
Sub([parameters])
sub indicates the begining of a block of Aptilis lines.
In Aptilis, a programme can be divided in subs, but only one
is necessary: the 'main' sub. That is the sub that is going to be run by default.
If you are running your script from the command line, you can retrieve the command line parameters by typing 'sub main(args[])' (see example 5).
A sub must be closed by end sub or end the_name_of_the_sub.
A sub can return a value with the 'return' keyword at any moment.
Returning values allows you
to use subs as functions (see example 2).
You do not need the dollar sign in the 'sub' line of a sub, but you need
to put the brackets '[]' to indicate that a given variable is going to be an
array. (see example 3)
Example 1:
(This is actually a complete programme)
sub main print("Hello World!\n") end mainResult:
Hello World |
Example 2:
(This is actually a complete programme)
sub square(param) return param * param end square sub main c = square(3) print(c) // we could now say 'end main', but 'end sub' is equally good. end subResult:
9 |
Example 3:
(Passing an entire array)
sub enumerate(param[]) n = getArraySize(param[]) for i=0 to n - 1 print(param[i]$, "\n") end for end enumerate sub main a[0] = "Homer" $ a[1] = "Marge" $ a[2] = "Bart" $ a[3] = "Lisa" $ a[4] = "Maggie" $ enumerate(a[]) end subResult:
Homer Marge Bart Lisa Maggie |
Example 4:
(Passing strings)
sub capitalize(n) c = asc(n$) if c >= 97 and c <= 122 c = c - 32 end if print(chr(c)$, mid(n$, 2)$) end capitalize sub main s = "hello" $ capitalize(s$) end mainResult:
Hello |
Example 5:
(Command line parameters)
Assuming the script is started with 'aptilis.exe test.e.txt param1 param2 param3' sub main(args[]) for i = 0 to getArraySize(args[])-1 print(args[i]$, "\n") end for end mainResult:
C:\APTILIS\APTILIS.EXE test.e.txt param1 param2 param3 |