Function - End Function

Function function_name ( [parameterlist] ) [as ReturnType]


[ function_name = return-value

[Exit Function] ]


function_name = return-value

End Function

A function is a block of nuBASIC statements enclosed by a declaration statement Function and a matching End Function.

You invoke a function from some other place in the code. This is known as a function call.

Parameter List

In most cases, a function needs to operate on different data each time you call it.

You can pass this information to the function as part of the function call.

The function defines zero or more parameters, each of which represents a value it expects you to pass to it.

Corresponding to each parameter in the procedure definition is an argument in the procedure call.

Arguments consist of variables, constants, and expressions that you pass to the procedure when you call it.

Returning from function

When the function is finished running, it returns control to the code that invoked it, which is known as the calling code.

The Function procedure also returns a value to the calling code. The calling code is a statement, or an expression within a statement, that specifies the procedure or the function by name and transfers control to it.

A procedure can return control to the calling code calling the appropriate Exit statement, or the procedure's End statement.

The value a Function sends back to the calling code is called its return value. It returns this value assigning a value to its own

function name in one or more statements of the procedure.

Control does not return to the calling program until an Exit Function or End Function statement is executed.

Calling Syntax

You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression, or with a stand-alone calling statement.

You must provide values for all arguments and you must enclose the argument list in parentheses.

The syntax for a call to a Function procedure is as follows:


lvalue = function_name( [argumentlist] ) : ' return-value is assigned to lvalue

Function is part of an expression such as any other built-in function

If function_name( [argumentlist] ) / 4 <> expression Then Print “OK”

Same calling syntax of Sub procedure in case you want to ignore return-value

function_name [argumentlist]

When you call a function that accepts one or more parameters you can pass the arguments by position, in the order in which the related parameters appear in the procedure's definition.


Function Sum(x as Integer, y as Integer) as Integer

Sum = x + y

End Function

Print Sum(10, 20)




A recursive procedure or function is one that has the ability to call itself until it detects that some base condition has been satisfied.

The following Function procedure uses recursion to calculate the factorial of its original argument.

Function fact%(n%)

If n%<=1 Then




End If

End Function

Remark: You should design a recursive procedure to test for at least one condition that can terminate the recursion, and you have also to handle the case where no such condition is satisfied within a reasonable number of recursive calls.

Without at least one condition that can be met without fail, your procedure runs a high risk of executing in an infinite loop.

Your application has a limited amount of space for local variables.

Each time a procedure calls itself, it uses more of that space for additional copies of its local variables.

If this process continues indefinitely, it eventually causes abnormal program termination.

See also Sub