Jump to content
Alodar

Function with Parameter values as a parameter

Recommended Posts

Alodar

So, i find myself often lately writing small loops to try doing a function multiple times (such as downloading a file, testing to see whether it downloaded correctly or not, and retrying if not because of various connection issues and timeouts), and I was thinking of writing a function to do it for me, but maybe it's bad practice?  For example:

Local $x = 0
While $x < 5

    $iReturnVal = _SomeFunc(param1, param2)

    If $iReturnVal = 1 then
        ExitLoop
    EndIf

    $x += 1
    
WEnd

 

But what I'd like to do is something more along the lines of this:

$iLoopVal = _TryLoop(_SomeFunc(param1, param2))

Func _TryLoop(_FuncName(Params))
    Local $x = 0
    While $x < 5

        $iReturnVal = _FuncName(Params)

        If $iReturnVal = 1 then
            Return 1
        EndIf

        $x += 1
        
    WEnd
    
    Return SetError(1, 0, 0)

EndFunc

 

So that I can just call that function without having to write the loop every time.  I suspect that that's probably bad coding, but i don't know.  Heck, I don't even know if it's possible.
 

So, first, can it be done, and second, if yes (or maybe even if no) should or should not I do that?

Share this post


Link to post
Share on other sites
jchd

You can make that:

$iLoopVal = _TryLoop(_SomeFunc, $param1, $param2)

Func _TryLoop($Fct, $p1, $p2)
    Local $x = 0
    While $x < 5
        $iReturnVal = $Fct($p1, $p2)
        If $iReturnVal = 1 Then Return 1
        $x += 1
    WEnd
    Return SetError(1, 0, 0)
EndFunc

Func _SomeFunc($parm1, $parm2)
    ; perform task here
EndFunc

Functions are now first class citizens in AutoIt.

If you want to accomodate a variable number of parameters without using ugly code, then use Call with its special feature (see help):

Local $aParms = [ _
    "CallArgArray", _       ; magic value
    $param1, _
    $param2 _
]
$iLoopVal = _TryLoop(_SomeFunc, $aParms)

Func _TryLoop($Fct, ByRef $aParams)     ; always only one argument here
    Local $x = 0
    While $x < 5
        $iReturnVal = Call($Fct, $aParams)      ; individual arguments inside array
        If $iReturnVal = 1 Then Return 1
        $x += 1
    WEnd
    Return SetError(1, 0, 0)
EndFunc

Func _SomeFunc(ByRef $aParmArray)
    ; perform task here
    ;
    ; first argument is $aParmArray[1]
    ; second argument is $aParmArray[2]
    ; last argument is $aParmArray[UBound($aParmArray)]
EndFunc

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×