GLBasic User Manual

Main sections

FUNCTION

FUNCTION name#$ : arg1#$, arg2#$...
...RETURN n#$
ENDFUNCTION



A function is a grouping of commands into a single entity. This group of commands can be run from elsewhere in your code simply by using the name given to that function. A function, unlike a subroutine, will return a value back to the calling code on completion.

Use the FUNCTION command to define user functions. name#$ is the name of the function. argx#$ are the arguments requested by the function. These variables are LOCAL to this particular function's workspace (i.e. they are created within the function, and deleted at its conclusion).
You can return a value from a function with the RETURN command. If you do not specify an return value, it will be '0'.

Functions can only return a single value back to the calling code. To use a function to update multiple values, see the BYREF command.

A function is invoked (called) when you use the name of the function in your code followed by brackets.
a=name(3,4)

will result in 'a' being assigned the value returned by the function 'name' when the arguments '3' and '4' are passed to it.

Example:
 
// Function

// Sample shows the usage of the commands
// FUNCTION / ENDFUNCTION / EXIT
// Due to the complicated syntax of a function
// the command 'New Function' should be used
// from the 'Project' menu to create a function.
GLOBAL a
LOCAL a

Title() // Title line

a$=Right$("123456", 2); // 56
PRINT a$, 100, 80

a=7; b=12; GLOBAL a=15
c=Maximum(a, b) // 7, 12
Center("c="+c, 100) // 12
Center ("a="+a, 140) // 7
Center("b="+b, 180) // 12
Center("GLOBAL a="+GLOBAL a, 220) // 0 (function: Max)
SHOWSCREEN
MOUSEWAIT

// ------------------------------------------------------------- //
// -=# MAX - größere Zahl(a, b) / bigger number(a, b) #=-
// ------------------------------------------------------------- //
FUNCTION Maximum: a, b
// These variables are defined LOCAL:
// a, b
LOCAL ret
IF a>b
ret=a
ELSE
ret=b
ENDIF
// This has no influence on the global variables
a=0
// This will
GLOBAL a=0
RETURN ret
ENDFUNCTION // MAX


// ------------------------------------------------------------- //
// -=# CENTER - Center text #=-
// ------------------------------------------------------------- //
FUNCTION Center: text$, y
// Diese Variablen sind als LOCAL definiert:
// There variables are defined LOCAL:
// text$, y
PRINT text$, (640-LEN(text$)*16)/2, y
ENDFUNCTION // CENTER


// ------------------------------------------------------------- //
// -=# TITLE #=-
// ------------------------------------------------------------- //
FUNCTION Title:
// There variables are defined LOCAL:
// retval
Center("Yippie", 50) // Function call within a function
RETURN // Exit the function
PRINT "Boo hoo", 100, 100 // Never happens
ENDFUNCTION // NAME


// A Function with a string as a return value
// ------------------------------------------------------------- //
// -=# RIGHT$ #=-
// ------------------------------------------------------------- //
FUNCTION Right$: word$, size
// These values are defined LOCAL:
// word$, size
RETURN MID$(word$, LEN(word$)-size, size)
ENDFUNCTION// RIGHT$


Output:
56
Yippie
c=12
a=7
b=12
GLOBAL a=0

See also...