Keyword Reference

Func...Return...EndFunc

Defines a user-defined function that takes zero or more arguments and optionally returns a result.

Func functioname ( [ByRef] $param1, ..., [ByRef] $paramN, $optionalpar1 = value, ...)
    ...
    [Return [value]]
EndFunc

 

Parameters

The parameters are set by you. You later call the function like any other built-in function.

 

Remarks

The ByRef keyword is optional and means: (1) the parameter must a variable, and (2) the variable could be changed by the function. By default, a parameter is passed by value which means that a copy of the parameter's value is manipulated by the function.

Use the Return keyword to exit the function. Unlike built-in functions, user-defined functions return 0 unless another return value is specified.

Arrays can be passed to functions (and returned from them) by simply using the array name without any brackets. Note that function declarations cannot appear inside other function declarations.

Optional parameters are defined by assigning to them a default value which can be a macro . They cannot be embedded with mandatory parameters. They are always the last in the definition.
Inside the the function the number of parameters used to called the function can be retrieved with the @NUMPARAMS macro.

 

Related

Dim/Global/Local, #include

 

Example

; Sample script with three user-defined functions
; Notice the use of variables, ByRef, and Return

$foo = 2
$bar = 5
msgBox(0,"Today is " & today(), "$foo equals " & $foo)
swap($foo, $bar)
msgBox(0,"After swapping $foo and $bar", "$foo now contains " & $foo)
msgBox(0,"Finally", "The larger of 3 and 4 is " & max(3,4))
Exit

Func swap(ByRef $a, ByRef $b)  ;swap the contents of two variables
    Local $t
    $t = $a
    $a = $b
    $b = $t
EndFunc

Func today()  ;Return the current date in mm/dd/yyyy form
    return (@MON & "/" & @MDAY & "/" & @YEAR)
EndFunc

Func max($x, $y)  ;Return the larger of two numbers
    If $x > $y Then
        return $x
    Else
        return $y
    EndIf
EndFunc

;End of sample script