FutureBasic Logo

<<    Index    >> FutureBasic 5

fn <userFunction>   function



Syntax:
[result =] fn functionName[(param1 [,param2 ...])]

Description:
Executes the user function specified by functionName, and optionally returns a numeric or string result. The user function must be one which was defined or prototyped at an earlier location in the program. A user function is defined using local fn statement. A user function is prototyped using the def fn <protoType> statement. If the user function returns a value, you can use fn <userFunction> as part of a numeric or string expression, as in this example:
count% = 3 * fn NumFish%(x) + 7
If the user function does not return a value, then you should use fn <userFunction> as a standalone statement.
If the function definition includes a list of parameters, then you must provide the same number of parameters (in param1, param2, etc.) when you call the function, and the parameters that you pass must be of compatible types. The compatible types are summarized here (not all of these are available for all kinds of functions; see the individual description of local fn):

Formal variable type (in FN definition)Compatible types (in Fn call)
signed/unsigned byte (var`; var``)Any numeric expression1,2
signed/unsigned short integer (var%;var%`)Any numeric expression1,2
signed/unsigned long integer (var&;var&`)Any numeric expression1,2
pointer variable (p As Pointer [To unType])A record variable, or a long-integer expression6
single/double precision floating point (var!; var#)Any numeric expression2
string variable(var$)Any string expression3
address reference (@adr&; @p As Pointer [To unType])Any variable (of any type), or a long-integer expression preceded by "=".4
array declaration (tableau[suffixe](dim1[,dim2...]))Base array element (arr[suffix](0[,0...]))5

Notes:
1. Non-integer values are rounded to integers before being moved into integer or pointer variables.
2. If you pass a numeric value that's outside the range of the formal variable type, you may get an unexpected result, or you may get an overflow error.
3. If you pass a string value that is longer than the maximum size of the formal variable, the string will be truncated.
4. If you specify a variable here, the variable's address will be copied into the formal parameter (addr& or p). If you specify a long-integer expression preceded by "=", then the value of that expression is copied into addr& or p.
5. The array must be a numeric or string array (not an array of records). All of the array's elements are accessible to the function. Any changes that are made to the array's elements within the function will also affect the array outside of the function. Note that if the array specified in the formal fn definition has a different type or different dimensions from the array you pass when you call the function, you may get unexpected results, or even a crash. (Be sure you know what you're doing before you try this!)
6. If you specify a record variable here, the record's address will be copied into the formal parameter (p). If you specify a long-integer expression, then the value of that expression is copied into p.
If the function definition does not have a list of parameters, then you must not include any parameters (nor parentheses) when you call the function.
In most cases, the parameters that you specify in fn <userFunction> are "passed by value." That means that the user function receives a private copy of the parameter's value; if the function changes that copy, it doesn't affect the value of the parameter that was used in the fn call.
In a few cases, the parameters that you specify in fn <userFunction> are "passed by reference." That means that the user function receives the address of the parameter that you specified. If the function changes the contents at that address, it will affect the value of the parameter you passed. Parameters are passed by reference when you use the following kinds of formal parameter declaractions in the function definition:
You can also pass the address of a variable or array by using the varptr function (varptr(var) or @var) when you call the function (if you do this, then specify a long integer variable or a pointer variable as the formal parameter in the fn definition). This is another way to give the function direct access to the memory comprising the variable or array, allowing it possibly to change its value.
Note that there is no way to pass the contents of a record directly to a function. To give the function access to a record's contents, either pass the record by reference, or pass the record's address directly (passing varptr(recVar) or @recVar when you call the function).
A fn <userFunction> call may appear anywhere below the place where the function is defined (or prototyped). It can appear in the "main" scope of the program, or inside other functions. It may even appear inside the very function that it is calling--this allows you to implement so-called "recursive" functions (functions which call themselves).

See Also:
local fn; def fn <prototype>