MC-Basic:FUNCTION ... END FUNCTION

From SoftMC-Wiki
Jump to: navigation, search

<name> is the function name.
<type_1> through <type_n> are the types of the parameters.
<p_1> through <p_n> are the names of the parameters passed to the function. Parameters are variables used inside the function code. When the function is called, the values of the variables are replaced by the calling values.

By default, parameters are passed by reference. {ByVal} is an option used to specify that the parameter is passed by value.

<function type> is the type of the return value

{local variables declaration} is used to declare variables local to the function (see variable scope, in this document).

{function code} defines the programming code of the algorithm which computes the value that is returned. The return value must be assigned to the <name> inside the function code.

The FUNCTION and END FUNCTION keywords are used to delimit a function within a task. The function(s) must appear after the main section of the code (delimited by PROGRAM….END PROGRAM). Local variables, defined using DIM, appear after the Function keyword, but before the code. Functions are executed by using the function name with its parameters in parentheses. Functions may be recursive.

Short form

FUNCTION_..._END_FUNCTION

Syntax

Function<name>({{ByVal}<p_1> as <type_1>} {,{ByVal}<p_n> as <type_n>}) As <function type>
{local variable declaration}
{function code}

END Function

Availability

All versions

Type

Parmeters: Long, Double, String, Joint, Location, user defined structures, Generic Axis, Generic Group, Moving Frame (by reference only), Cam (by reference only), Comp (by reference only), Semaphore (by reference only), user defined Error and Note (by reference only).

Returned Values: Long, Double, String, Joint, Location, user defined structures, Generic Axis, Generic Group.

Scope

Task

Limitations

Write only. Arrays are only passed by reference.

Examples

Program
       ?add1(5)

End Program

Function add1(byval a as long) as long
          Add1=a+1

End Function                                                ' running this program prints 6

See Also