Sign in to follow this  
Followers 0
Codemike

How to use functions?

4 posts in this topic

Why my program don't work? Only first two functions work and I don't get any error mesages. I'm also interesting how can do loop in functions (start program again). I'm a beginner.

Main()

Func Main()

$clipboardContents = Clipget()

$Text = $clipboardContents

If ContainsSuchword($Text) Then

MsgBox(0, "", "There is one or more such words")

Else

MouseClick("left",707,553)

EndIF

EndFunc

Func ContainsSuchWord($Text)

Dim $Suchwords[3] = ["computer", "sun", "we were"]

For $i = 0 To UBound($Suchwords)-1

If StringInStr($Text, $Suchwords[$i]) Then

Return True

EndIf

Next

Return False

EndFunc

Func MainB()

$clipboardContentsB = Clipget()

$TextB = $clipboardContentsB

If ContainsSuchwordB($TextB) Then

MsgBox(0, "", "There is one or more such B words")

Else

MouseClick("left",707,553)

EndIF

EndFunc

Func ContainsSuchWordB($TextB)

Dim $SuchwordsB[3] = ["car", "apple", "radio talk"]

For $i = 0 To UBound($SuchwordsB)-1

If StringInStr($Text, $SuchwordsB[$i]) Then

Return True

EndIf

Next

Return False

EndFunc

MsgBox(0,"", "The End")

Share this post


Link to post
Share on other sites



You are never calling the other 2 functions. :)


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

You are never calling the other 2 functions. :)

That and ContainsSuchWordB using $Text instead of $TextB are the only real problems, but I went a bit OCD and commented the script with some additional suggestions, that make little functional difference, but produce easier to read code.

While 1 ;This is an infinite loop. Never write one without some way to get out of it.
    Main() ;In AutoIt you don't need to put the main part of the script in Main(). You still can if you want though.
    ;MainB() ;You could have called MainB() here, but I'd use another approach shown below.
    If MsgBox(5,"Loop","Retry, or cancel?") = 2 Then ExitLoop ;this is a way to end an (otherwise) infinite loop.
WEnd ;At this point the loop starts again.
MsgBox(0, "", "The End") ;Try to keep all code together, as opposed to split up by function declarations.

Func Main()
    Local $sClipboard = ClipGet() ;No need to create extra variables that go unused. I also like to specify what is in the variable. "sVarNAme" for strings.
    If ContainsSuchword($sClipboard) Then
        MsgBox(0, "", "There is one or more such words")
    Else
        MouseClick("left", 707, 553)
    EndIf
    If ContainsSuchWordB($sClipboard) Then ;I combined both main functions. You could also have called MainB() just after Main() returns. As shown on (line 3)
        MsgBox(0, "", "There is one or more such B words")
    Else
        MouseClick("left", 707, 553)
    EndIf
EndFunc

Func ContainsSuchWord($sText)
    Local $asSuchWords[3] = ["computer", "sun", "we were"] ;Try to avoid Dim. Specify Local, or Global instead. (Using "asVarName" to specify an array of strings.)
    For $i = 0 To UBound($asSuchWords) - 1
        If StringInStr($sText, $asSuchWords[$i]) Then Return True ;You could make this single line if you prefer.
    Next
    Return False
EndFunc

Func ContainsSuchWordB($sText) ;No need to use other varable names if you use locals.
    Local $asSuchWords[3] = ["car", "apple", "radio talk"]
    For $i = 0 To UBound($asSuchWords) - 1
        If StringInStr($sText, $asSuchWords[$i]) Then ;you where still using $Text here instead of $TextB.
            Return True
        EndIf
    Next
    Return False
EndFunc

Share this post


Link to post
Share on other sites

Thanks very, very much. So kind of you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0