Jump to content

Optional ByRef parameter


Recommended Posts

/* 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

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

Link to comment
Share on other sites

  • 4 years later...

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)  ?

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...