Community QBasic

Rebooting the QBasic we all know and love!


Project maintained by Cory Smith

DEF FN

Defines a function.

Syntax

DEF FNname [ (argument_list) ] = expression`

DEF FNname [ (parameter_list) ] … FNname = expressionEND DEF

Comments

Function names must beging with FN and can contain up to 40 characters. The function name indicates the value type the function returns:

Function Name Returns
FNday$ string
FNcount% integer
FNaverage# single-preceision value

For a function to return a value, the function must assign its result to the function name.

argument_list is a list of parameters to the function separated by commas as follows:

argument_name [ AS type ]

You cannot use a function before your program defines it, nor can you use DEF FN functions recursively.

Example

DEF FNsum (a AS INTEGER, b AS INTEGER) = a + b

DEF FNmax (a AS INTEGER, b AS INTEGER, c AS INTEGER)
  IF (a > b) THEN
    max = a 
  ELSE 
    max = b 
  END IF
  IF (max > c) THEN
    FNmax = max
  ELSE
    FNmax = c 
  END IF 
END DEF

PRINT FNsum(3, 5)
PRINT FNmax(1, 2, 3)

Running this program produces the following:

8
3