Jump to content

Making your own functions


Guest weirdwes
 Share

Recommended Posts

Guest weirdwes

Here is what I want to do, but keep in mind this doesn't work as is.

$count = 1
Inc($count)
Dec($count, -8)
MsgBox(0,"Counting Fun","Your $Count now equals: " & $count)

Func Inc(BYREF $var, $step)
     IF $step = 0 then
          $step = 1
     Endif
     $var = $var + $Step
EndFunc

Func Dec(BYREF $var, $step)
     IF $step = 0 then
          $step = 1
     Endif
     $var = $var - $Step
EndFunc

The second line fails with an error because I am not passing the $step parameter for the function. How can I make this optional and set it to 1, if it's not included?

Link to comment
Share on other sites

AutoIt does not support optional parameters in user-defined functions..... :whistle:

Workarounds:

1) Pass an array as the single argument, and compute based on the number of elements. (Won't help in this particular example.)

EDIT: I don't know why I was using Return in my original code post. Fixed below:

2) Use two slightly different function names.

$x = 3
$y = 3
MsgBox(0,"", Inc1($x))
MsgBox(0,"", Inc($y, 2)

Func Inc1(byRef $var)
   $var = $var + 1)
EndFunc

Func Inc(byRef $var, $amount)
   $var = $var - $amount
EndFunc

P.S. Dec($count, -8), the way you coded it, will add 8.

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

The Optional is in David/JP (Myself) dbg-bastard perhaps Jon will have time to integrate it

Func functioname ([byRef]$param1, ... [,Optional [byRef] $paramN+1, ... ])

    statement1

    ...

EndFunc

:whistle:
Link to comment
Share on other sites

You will love funtions, they are like little subroutines, and are very needed since we got rid of the V2 Goto command.

oldstyle:

send,hi
if,winexist,mywindow,,goto,sayhi
back:
end

sayhi:
send,hi
goto,back

New style:

send("hi")
if winexists("mywindow","") then sayhi()
exit

func sayhi()
send("hi")
endfunc

They get a lot more complex, and can return values, or work as subroutines. Used in conjunction with Do...Until ,For...Next ,While...WEnd , etc, they come in real handy, and can acomplish much more than the predicessor.

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...