FUNCTION name (optional parameters)

FUNCTION name  [AS INTEGER | STRING | SINGLE]     ' a FUNCTION without parameters


FUNCTION name (parameter list)  [AS INTEGER | STRING | SINGLE]
  parameter list = parameter [, parameter list]  
                   |   PARAMARRAY paramarray
  parameter  =  [BYVAL] paramname [AS INTEGER] 
                   |   [BYVAL] paramname AS SINGLE
                   |   [BYVAL] paramname(size) AS STRING
                   |   BYREF paramname AS STRING 
                   |   BYREF paramname  [AS INTEGER]
                   |   BYREF paramname AS SINGLE


FUNCTIONs are an extension of SUB that will return a value.  If no type for the FUNCTION is specified, then INTEGER is assumed.

The FUNCTION .. ENDFUNCTION construct allows for a second scope of variables.  Scope meaning the region in which code can see a set of labels.  ARMbasic has a global scope and a local scope for any variable declared with DIM inside an FUNCTION. Local scope variables will be only accessible from within that FUNCTION procedure (the local scope).

Simple parameters are assumed to be called BYVAL if not specified.  In BYVAL calls, a copy of the parameter is passed to the FUNCTION procedure.  Integer or string parameters may be called BYREF which means a pointer to the integer/string is passed, and changes to that integer/string can be made by code inside the FUNCTION procedure.  STRINGs may be passed either BYVAL or BYREF, but arrays must be passed BYREF.

Code labels for goto/gosub declared within the SUB procedure are also in the local scope. Call to global labels are allowed within a FUNCTION ... END FUNCTION , but that global label must be defined BEFORE the FUNCTION ... END FUNCTION .

A FUNCTION should end with a RETURN expression, as FUNCTIONs return a value, if that is not needed use a SUB.. A FUNCTION may also be called with a GOSUB, but the returned value is ignored.

FUNCTION without any parameters can be called with or without (), an empty parameter list.

Recursive calls with parameters or local variables are not supported.  And ENDFUNCTION or END FUNCTION syntax are allowed.

Program structure:

FUNCTIONs  should be arranged ahead of the MAIN: body of code.  In many cases they will be part of #include files at the beginning of the user ARMbasic code.  If FUNCTIONs are located at the start of a program a MAIN: must be used.

FUNCTIONs  can access global variables that have been declared before the FUNCTION, this declaration can either be implicit or use a DIM.

FUNCTIONs must be defined before they are called. 


function toupper(a(100) as string) as string 
  dim i as integer 
  for i=0 to 100
    if a(i)=0 then exit 
    if a(i) <= "z" and a(i) >= "a" then a(i) = a(i) - &H20
  next i
  return a
end function


print toupper("asdf")                   '  will print ASDF

Differences from other BASICs

See also