Adv HW

From AutoIt Wiki
Jump to navigation Jump to search

Advanced Hello World

Full Script:

Func GenTitle () Local $Title = "H" & 'e' $Title &= "ll" $Title = $Title & "o" Return $Title EndFunc Func GenMsg (ByRef $msg) $msg &= "orld" EndFunc Global $MsgBox = "W" GenMsg($MsgBox) MsgBox(1, GenTitle (), $MsgBox)

This is our finished product above, it uses Functions, ByRef, etc...

Func GenTitle () This is a simple no parameter Function Line, you can rename GenTitle to anything, but you have to make sure the rest of the script is updated aswell.

Local $Title = "H" & 'e' This is a variable statement, I'm going to break it down a bit. Local This tells Autoit where the variable will work, if it's "Local" then it will only work within the function you called it in.

$Title This is the actual Variable, The $ sign always has to be before the name of the variable.

= This is the operator to show Autoit that the variable is going to hold the information to the right.

"H" This is the first of the text that is going to go into the variable, the Quote marks are needed to show what you want to put into the variable, you may also have this to be 'H'.

& This is another useful operator to tell Autoit that you want to join H and e together

'e' This is another bit of text were putting in $Title, it shows the use of '' instead of ""

So as of now, we have a variable named $Title with the text He in it.

$Title &= "ll" This is again using the variable $Title and were adding more text to it this time.

&= This is a handy operator to join the text to the right into the variable. The variable will now hold Hell instead of He

$Title = $Title & "o" This is another way to do the same thing as &=, all this is doing is finishing Hello

Return $Title This will return Hello whenever it is called. If we were to put $Hello = GenTitle (), $Hello would contain Hello

EndFunc This shows autoit that we are done our function, you always need to have a EndFunc to pair to a Func.

Func GenMsg (ByRef $msg) This is a more complex Function statement, it contains a parameter ($msg) but it also uses ByRef.

ByRef $msg ByRef is a way of bringing a variable into a statement, any changes you make to that variable stay even after the function is done. So if you were to have a variable that has a A in it and you were to ByRef it into a func that adds BC then end the function, your variable will contain ABC.

$msg &= "orld" This simply adds orld to the variable you have given the function.

EndFunc This shows autoit that we are done our function, you always need to have a EndFunc to pair to a Func.

Global $MsgBox = "W"

This is another declare variable statement, but it uses Global this time. Global will allow $MsgBox to be used throughout the script, in functions and all.

GenMsg($MsgBox) This calls our previously made function GenMsg. It will use the variable $MsgBox with a W in it so that the function will add orld and together makes World.

MsgBox(1, GenTitle (), $MsgBox) Our first internal function! This will display a messagebox with Hello as the title (Since GenTitle() will return Hello) and $MsgBox will hold World (The ByRef added orld to the w that we declared.)

That's it for our advanced Hello World Script! Hopefully it helped you understand some Autoit concepts. If you see any errors in it, please edit them out or PM me on the forums to get them changed!

--Chip 21:02, 10 July 2007 (PDT)