Инструменты пользователя

Инструменты сайта


en:autorun:documentation:func_endfunc

Func...EndFunc

Func functionname([Param1 .. ParamN])
 
EndFunc

Specifies a user defined function.

Function parameters

4 types of formal parameters can be set in functions:

  • Simple parameter
  • Parameter «by reference»
  • Parameter with default value
  • Open parameter list

Simple parameter

A simple parameter is given simply by the name of the variable. The value of the actual parameter will be calculated before being passed to the function.

MyFunc(123+456)
 
Func MyFunc(var)
    MsgBox(var) # --> 579
EndFunc

Parameter with default value

This parameter is given by a variable name with a default value assigned. This allows you to omit some of the parameters when calling the function. Parameters of this type must come after the simple parameters and «by reference» parameters, but before the open list of parameters. If the actual parameter is set, then the variable will be assigned its value, if not, the default value.

MyFunc("text")
MyFunc("text", "My title")
 
Func MyFunc(var1, var2 = "Default title")
    MsgBox(var1) # --> text --> text
    MsgBox(var2) # --> Default title --> My title
EndFunc

"By reference" parameter

This type of parameter enables the function to return back the modified parameter. This is useful to reduce the number of global variables, which in turn reduces the number of possible errors.

This parameter is specified by the variable name after the ByRef keyword. The value of the actual parameter must also be a variable, moreover, existing at the time the function is launched, otherwise an error message will be displayed. The value of the current variable is passed to the function and the function can change it.

a = 1
 
MsgBox(a) # --> 1
 
MyFunc(a)
 
Func MyFunc(ByRef var)
    MsgBox(var) # --> 1
    var = 123
EndFunc
 
MsgBox(a) # --> 123

Open parameter list

The parameter is specified by the special value «…» and allows you to specify an unlimited list of input parameters. This parameter must be at the very end of the list of formal parameters, everything after it will be ignored. Receiving the passed parameters is done using the special function Args:

MyFunc(123, 456, 789)
 
Func MyFunc(...)
    For i = 1 To Args()
        MsgBox(Args(i), i) # --> 123 --> 456 --> 789
    Next
EndFunc

Related

Return, Args

en/autorun/documentation/func_endfunc.txt · Последнее изменение: 2022/09/26 17:44 — loopback

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki