Q(uick)BASIC Statement: FUNCTION

Quick View

FUNCTION...END FUNCTION

A a non-executable statement that declares the name, the parameters, and the code that form the body of a FUNCTION procedure

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • FUNCTION name [(parameterlist)] [STATIC][statementblock]name = expression[statementblock]END FUNCTION
Description/Parameter(s)
name The name of the function and the data type it returns, specified by a data-type suffix (%, &, !, #, or $).
parameterlist One or more variables that specify parameters to be passed to the function when it is called:
  • variable[( )] [AS type] [, variable[( )] [AS type]]…
variableA Basic variable name.
typeThe data type of the variable (INTEGER, LONG, SINGLE, DOUBLE, STRING, or a user-defined data type).
STATIC Specifies that the values of the function's local variables are saved between function calls.
expression The return value of the function.
  • When you call the function, you can specify that an argument's value will not be changed by the function by enclosing the argument in parentheses.
Example

The program REMLINE.BAS illustrates calling FUNCTION procedures. To view or run this program, load REMLINE.BAS using the Open command from the File menu.

Syntax
  • FUNCTION name [(parameterlist)] [STATIC][statements]name = expression[statements]END FUNCTION
Description/Parameter(s)
Part Description
name The name of the function. FUNCTION names follow the same rules as BASIC variable names and can include a type-declaration character (%, &, !, #, or $). Note that the type of the name determines the type of value the function returns. For example, to create a function that returns a string, you would include a dollar sign in the name or give it a name defined as a string name by a DEFSTR statement.
parameterlist The list of variables, separated by commas, passed to the FUNCTION. The parameters are passed by reference, so any change to a parameter's value inside the function changes its value in the calling program.
STATIC Indicates that the function's local variables are to be saved between calls. Without STATIC, the local variables are allocated each time the function is invoked, and the variables' values are lost when the function returns to the calling program. The STATIC attribute does not affect variables that are used in a FUNCTION but declared outside the FUNCTION in DIM or COMMON statements using the SHARED attribute.
expression The return value of the function. A FUNCTION returns a value by assigning a value to the function name. If no value is assigned to the FUNCTION name, the FUNCTION returns a default value: a numeric function returns a value of zero, and a string function returns the null string ("").

A parameterlist has the following syntax:

  • variable[( )][AS type][,variable[()][AS type]]

A variable is any valid BASIC variable. The optional type can be either INTEGER, LONG, SINGLE, DOUBLE, STRING, or a user-defined type.

Earlier versions of BASIC required the number of dimensions in parentheses after an array name. The number of dimensions is no longer required. Only the parentheses are required to indicate the parameter is an array. For example, the following statement indicates that both Keywords$ and KeywordTypes are arrays:

  • FUNCTION ParseLine(Keywords$(),KeywordTypes())

A FUNCTION procedure is like a SUB procedure: it can accept parameters, perform a series of statements, and change the values of its parameters. Unlike a SUB, a FUNCTION is used in an expression in the same manner as a BASIC intrinsic function.

Like SUB procedures, FUNCTION procedures use local variables. Any variable not in the parameter list is local to the FUNCTION unless it is declared as a shared variable in a SHARED statement, or unless the variable appears in a DIM or COMMON statement with the SHARED attribute.

To return a value from a function, assign the value to the function name. For example, in a function named BinarySearch, you might assign the value of the constant FALSE to the name to indicate the value was not found:

  • FUNCTION BinarySearch(…)
  • CONST FALSE=0
  • ' Value not found. Return a value of FALSE.
  • IF Lower>Upper THEN
  • BinarySearch=FALSE
  • EXIT FUNCTION
  • END IF
  • END FUNCTION

Using the STATIC keyword slightly increases execution speed. STATIC is not usually used with recursive FUNCTION procedures.

The EXIT FUNCTION statement provides an alternative exit from a FUNCTION. See the EXIT statement.

Because BASIC may rearrange arithmetic expressions to attain greater efficiency, avoid using FUNCTION procedures that change program variables in arithmetic expressions. Also avoid using FUNCTION procedures that perform I/O in I/O statements.

QuickBASIC FUNCTION procedures are recursive - they can call themselves to perform a given task. See the second example below.

Example

FUNC_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the use of the FUNCTION statement. To look at the program in the View window and, optionally, to run the program, load FUNC_EX.BAS using the File menu's Open Program command.
The program uses a recursive function (a function that calls itself) to find the length of a string.

Syntax
  • FUNCTION name [(parameterlist)] [STATIC][statementblock]name = expression[statementblock][EXIT FUNCTION][statementblock]END FUNCTION
Description/Parameter(s)
  • The type of name determines the type of value the function returns. For example, to create a function that returns a string, you would include a dollar sign in the name or give it a name defined as a string name by a DEFSTR statement. FUNCTION procedure names follow the rules for naming BASIC variables.
  • Names in the parameterlist are separated by commas. The syntax of parameterlist is:

  • [BYVAL] variable[( )] [AS type] [,[BYVAL] variable[()] [AS type]]…
BYVAL Defines the variable as being passed by value rather than by reference (the default). BYVAL can be used only with simple numeric types (INTEGER, LONG, SINGLE, DOUBLE, or CURRENCY).
variable A BASIC variable name. Previous versions of BASIC required the number of dimensions in parentheses after an array name. In the current version of BASIC, the number of dimensions is not required.
AS type The type of the variable: INTEGER, LONG, SINGLE, DOUBLE, STRING, CURRENCY, or a user-defined type. You cannot use a fixed-length string as a parameter unless it is contained in an array. You can use a simple fixed-length string as an argument in a CALL statement; BASIC converts a simple fixed-length string argument to a variable-length string argument before passing the string to a function.
  • STATIC indicates that the function's local variables are to be saved between calls. Without STATIC, the local variables are allocated each time the function is invoked, and the variables' values are lost when the function returns to the calling program. The STATIC attribute does not affect variables that are used in a FUNCTION procedure but declared outside the FUNCTION procedure in DIM or COMMON statements using the SHARED statement.
  • The expression is the return value of the function. A FUNCTION procedure returns a value by assigning a value to the function name. If no value is assigned to the FUNCTION name, the FUNCTION procedure returns a default value: a numeric function returns 0, and a string function returns the null string ("").
  • EXIT FUNCTION immediately exits a FUNCTION procedure. Program execution continues where the function was invoked. EXIT FUNCTION can be used only in a FUNCTION procedure. For more information, see the EXIT .

Usage Notes

  • If a parameter is passed by reference, any change to the parameter's value inside the procedure changes its value in the calling program.
  • If a parameter is passed by value, any changes to the parameter's value inside the procedure is local to that procedure and does not affect its value in the calling program.
  • Earlier versions of BASIC required the number of dimensions in parentheses after an array name. The number of dimensions is no longer required. Only the parentheses are required to indicate the parameter is an array. For example, the following statement indicates that both Keywords$ and KeywordTypes are arrays:

  • FUNCTION ParseLine (Keywords$(), KeywordTypes())

  • Earlier versions of BASIC did not allow you to pass arrays containing fixed-length strings as parameters. BASIC now supports this.
  • A FUNCTION procedure is like a SUB procedure: it can be defined with parameters, perform a series of statements, and change the values of its parameters. A FUNCTION procedure is always used in an expression in the same manner as a BASIC intrinsic function.
  • Like SUB procedures, FUNCTION procedures can use local variables. Any variable referred to within the body of the function is local to the FUNCTION unless one of the following conditions is true:
    • The variable is declared within the function as a shared variable in a SHARED statement
    • The variable appears in a DIM or COMMON statement with the SHARED attribute at the module level
  • To return a value from a function, assign the value to the function name. For example, in a function named BinarySearch, you might assign the value of the constant FALSE to the name to indicate the value was not found:

  • FUNCTION BinarySearch(…)
  • CONST FALSE=0
  • ' Value not found. Return a value of FALSE.
  • IF Lower>Upper THEN
  • BinarySearch=FALSE
  • EXIT FUNCTION
  • END IF
  • END FUNCTION

  • Using the STATIC attribute increases execution speed slightly. STATIC is usually not used with recursive FUNCTION procedures.
  • Because BASIC may rearrange arithmetic expressions to attain greater efficiency, avoid using FUNCTION procedures that change program variables in arithmetic expressions. Also, avoid using FUNCTION procedures that perform I/O.
  • FUNCTION procedures can be recursive--they can call themselves to perform a given task. See the example for more information.

Important

  • Avoid using I/O statements in a FUNCTION procedure called from an I/O statement; they can cause unpredictable results. Also, because BASIC may rearrange arithmetic expressions to attain greater efficiency, avoid using FUNCTION procedures that change program variables in arithmetic expressions.
Example

This example uses a recursive FUNCTION procedure (a function that calls itself) to find and return the length of a string.

DECLARE FUNCTION StrgLng% (X$) CLS INPUT "Enter a string: "; InString$ PRINT "The string length is"; StrgLng%(InString$) FUNCTION StrgLng% (X$) IF X$ = "" THEN 'The length of a null string is zero. StrLng% = 0 ELSE 'Non-null string--make a recursive call. 'The length of a non-null string is 1 plus 'the length of the rest of the string. StrgLng% = 1 + StrgLng%(MID$(X$, 2)) END IF END FUNCTION