Function

=Function=

Run BASIC supports user-defined functions. They are defined with **Function** and they must end with an **End Function** statement. Variables inside functions are local to the function. Variables in the main program are not visible inside the function unless they have been declared Global. Functions are different from subs because they **return a value.**

Syntax
The syntax for these statements is as follows:

FUNCTION fn([param1[, param2...]]) //- Define a function named fn with optional parameters// END FUNCTION //- Specify the end of an IF/SUB/FUNCTION/SELECT/DIV block//

The parameter list is separated by commas. The parameters are surrounded by parentheses. Paramenters can be both string and numeric types. A function needn't have any parameters at all.

The code listing should not fall into a function. A Wait statement in the line before the **Function** definition guarantees that the program will not procede into the **Function**.

Invoking a Function
User-defined functions are invoked in the same way as native Run BASIC functions. The returned value may be assigned to a variable, printed, or used in a larger expression.

Here is a generic function.

code print MyFunction$(var$) wait

function MyFunction$(a$) 'do something with a$ MyFunction$ = b$ end function code

Returned Value
Functions return a value. You specify this return value by assigning it to the function name before the **End Function** statement. If your function name is "MyFunction$" then the value is returned by assigning it to "MyFunction$" as is done below.


 * MyFunction$ = b$**

String or Numeric?
Functions that return a string (text) value must have names that end with a $ character. Functions that return a number have names that do not end in the $ character.

Sample string function names: code WholeName$ PathOnly$ MyFunction$ code

Sample numeric function names: code AddOne DoMath LengthOfTime code

Numeric Example
The following demo invokes the AddOne function, sending in a value of 4. Notice the **Wait** statement before the function statement. This insures that the code doesn't fall into the function accidentally. The function uses a variable called x to hold the value sent to it when you invoke it. It then adds 1 to x and returns the result. It does this by assigning the value of x+1 to the function name, "AddOne".

code format="vbnet" print AddOne(4)

WAIT

Function AddOne(x) AddOne = x + 1 End Function code

The displayed result is:

code 5 code

The next demo also invokes the AddOne function, but this time it is used as part of a larger expression.

code format="vbnet" print 7 + AddOne(4)

WAIT

Function AddOne(x) AddOne = x + 1 End Function code

The displayed result is:

code 12 code

String Example
The following example sends two strings into the function called "Concatenate$". It assigns the returned value to "var$". The function concatenates the two string variables called one$ and two$ and places an empty space between them. It assigns the value of the concatenated string to the name of the function.

code format="vbnet" var$ = Concatenate$("Carl","Gundel") print var$

WAIT

Function Concatenate$(one$,two$) Concatenate$ = one$ + " " + two$ End Function code

The result looks like this:

code Carl Gundel code

The functions in the previous examples are quite simple. Here's a more complicated example. It extracts the extension from a filename.

code format="vbnet" print Extension$("readme.txt")

WAIT

Function AddOne(x) AddOne = x + 1 End Function

Function Concatenate$(one$,two$) Concatenate$ = one$ + " " + two$ End Function

Function Extension$(f$) index = len(f$) while index>0 index = index-1 e$ = mid$(f$,index,1) if e$ = "." then exit while wend

Extension$ = right$(f$,len(f$) - index) End Function code

Mixed Argument Types
Arguments for both string an numeric functions can be both string and numeric. Here is a string function with both a string and a numeric argument.

code format="vbnet" print MakeBigString$("a",17)

WAIT

Function MakeBigString$(var$,x) for i = 1 to x MakeBigString$ = MakeBigString$ + var$ next End Function code

The result:

code aaaaaaaaaaaaaaaaa code