Functions - o2bblockbusting/cs421_project GitHub Wiki

Functions are defined using a combination of the 'A', 'V', 'F', and 'R' keywords. The 'R' stands for return and is used inside the function to return a value. The other three characters are used at the start to define a function. Here is the general structure of a function definition, with square brackets being used to show what parts the user needs to fill in. Note that all white space shown is optional.

A [function name] F [return type; ie B,C,D,G,N,L,O,S or nothing] V ([data type 1] [parameter 1], [data type 2] [parameter 2 etc]) Y [function body] Z

You can call a function using its name, followed by parenthesis with the required arguments. If the function call is standalone and not part of an assignment, it must be preceded with a 'V' to separate it from the previous statement.

This may be quite hard to understand, so I would recommend looking at the some examples instead.

Examples

This is a simple function that prints hello world and has no arguments or return value.

AhelloFV()Y
P("Hello World!\n")
Z

Vhello()   M# Call the function #M

This is a factorial function that takes in an integer x as its input and returns a long:

AfacFLV(Nx)Y
Ix=0|x=1Y
R1
ZEY
Rx*fac(x-1)
Z
Z

P(fac(7))   M# Display factorial of 7 #M
P("\n")