|
Function Statement |
Top Previous Next |
Function: |
Used to define functions -- distinct units in code which perform specific tasks and always return a value. |
Syntax: |
[ public ] function name [ ( [ byref ] argument1 as type1, [ byref ] argument2 as type2…) ] as ret_type statement1 statement2 … [exit function] … statementN end function |
Scope: |
Global |
See Also: |
Part |
Description |
public |
Optional. If set, allows other compilation units (files in a project) to access the function. |
name |
Required. Specifies the name for the function (used to call it, etc). |
byref |
Optional. If present, the argument immediately following this modifier will be passed by reference. |
argument[1, 2...] |
Optional. The name of the argument(s) passed to the function; arguments must have a name which is a valid identifier. This is a local identifier, used to refer to these arguments within the body of the function. |
as |
Optional (required if arguments are specified). Precedes the type definition. |
type[1, 2...] |
Optional (required if arguments are specified). Specifies the data type for the argument. Each argument name must be followed with a type definition, even when specifying several arguments of the same type. |
ret_type |
Required. Specifies the type of the value the function will return. In effect, this is the data type of the function. |
statement[1, 2...] |
Required. The body of code executed within the function; specifies the actual 'work' done by the function. |
Details
Functions cannot be nested (which is why their scope is defined as global or HTML). Function always return a single value. Functions can call other functions and subroutines.
The return value of a function must be explicitly set from within the body of the function, by referring to the name of the function as a variable (of type ret_type) which is then assigned a value.
Examples
'this is just an example to show how functions call each other. It's not actually useful. declare subtract (x as byte, y as byte) as integer ' have to declare, because it's invoked before its body.
function distance(x as byte, y as byte) as integer if x>y then distance = subtract(x,y) else distance = subtract(y,x) end if end function ... function subtract (x as byte, y as byte) as integer subtract = x - y end function
|