By using this site you agree to the use of cookies.

Learn more

Share via:

Creating Functions in AutoIt

Tutorial on user made functions in AutoIt, the example shows a simple user made function outputting a message box when called.

Edited: 2017-03-25 02:16

AutoIt Logo

AutoIt allows the creation of custom functions, which can be useful when maintaining larger scripts.

Creating your own functions can increase the maintainability of your scripts by avoiding to repeat code, and keep code intended for one action inside a single function.

Creating a function

A function can perform almost any action you would like. To keep things simple, the below simply shows a message box when called.

MessageFun()

Func MessageFun()
  MsgBox(4096, "Result", "The message box function was called")
EndFunc

More advanced functions sometimes accept input, called arguments, or parameters.

Using function arguments

Arguments can be used to pass data on to your functions from elsewhere in your script.

To allow a function to accept input through Arguments, simply place variables in between the parentheses of the function.

To pass just a single argument to your function, do like this:

MessageFun('This is some message')

Func MessageFun($Msg)
  MsgBox(4096, "Result", $Msg)
EndFunc

Above function could used to output a message, could be a error message, or whatever you have going on.

To make the function accept multiple values, simply separate the arguments with comma, like this:

MessageFun('Error: Unable to install file: ', '(Name of File)')

Func MessageFun($Msg, $Msg2)
  MsgBox(4096, "Result", $Msg & $Msg2)
EndFunc

Also note that we used the ampersand to join the two strings in $Msg and $Msg2, this is referred to as String Concatenation, you can read more about that in the tutorial on AutoIt Variables.