Sign in to follow this  
Followers 0
dmitryunruh

Optional ByRef parameter

5 posts in this topic

/* AutoIt3 Script Example */

Func MyFunc (ByRef $param = 0)

ConsoleWrite ("Something" & @CRLF)

EndFunc

/* AutoIt3 Syntax Checker Output */

D:\DU\develop\AutoIt v3 Script.au3(8,27) : ERROR: syntax error

Func MyFunc (ByRef $param =

~~~~~~~~~~~~~~~~~~~~~~~~~~^

D:\DU\develop\AutoIt v3 Script.au3 - 1 error(s), 0 warning(s)

>Exit code: 2 Time: 0.207

/* Question */

Why ByRef parameter cannot be Optional?

Or

Why Optional parameter cannot be ByRef?

-------------------------------------------------

AutoIt v3.3.4.0 and v3.3.5.4

Share this post


Link to post
Share on other sites



Welcome to the forums dmitryunruh.

You cannot do this because the way the function works. ByRef requires a variable as an input. So what happens in the end you are passing that variable to be used inside the function and subsequently to be change inside that function.

Like so:

;$i = 1
$i = 1
MsgBox (0, "Value of $i", $i)
;$i = 11
$i = $i + 10
MsgBox (0, "Value of $i", $i)
;$i = 13
Plus2 ($i)
MsgBox (0, "Value of $i", $i)

;Function to make number increase by 2
Func Plus2 (ByRef $iNumber)
    $iNumber += 2
EndFunc

If you want it option you could do something like follows:

Global $iNumber = 1
;$iNumber= 1
$iNumber= 1
MsgBox (0, "Value of $iNumber", $iNumber)
;$iNumber= 11
$iNumber= $iNumber+ 10
MsgBox (0, "Value of $iNumber", $iNumber)
;$iNumber= 13
Plus2 ()
MsgBox (0, "Value of $iNumber", $iNumber)

;Function to make number increase by 2
Func Plus2 ()
    $iNumber += 2
EndFunc

What is your function? What is it's purpose?

Cheers,

Brett

Share this post


Link to post
Share on other sites

Reviving an old thread but it seems to be the most recent one treating the topic:

What is your function? What is it's purpose?

 

 

Possible example: I am looking for an image in the screen. By default I want to search the entire screen, but sometimes I want to pass an extra optional parameter with an array of 'search areas' (each search array is again an array with coordinates).  It would be more performant if I could pass that optional parameter ByRef.... (especially since my top level search function goes about 5 levels deep by the time the actual searching is done, so the copy operations start to add up).

If I don't want to pass the search areas I would use the default value 0 (cfr a NULL pointer) and test with IsArray or something....

Any tricks to realize this in Autit (apart from creating 2 functions or going the byval route as I currently do)  ?

Share this post


Link to post
Share on other sites

Create a wrapper function without the "optional" array as an input, just calling the original function with an empty 0 elements array on the array input or something?

Share this post


Link to post
Share on other sites

I suggest to create a new thread. The OP was using AutoIt v3.3.4.0 and v3.3.5.4. A lot has changed since then :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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