Jump to content

Function call with ByRef


Recommended Posts

Hi,

there is something that I do not understand. I wanted to address the index in an array by increasing the index variable using a function call with ByRef. But the result seems strange to me:

#Region Includes
    #include <Array.au3>
#EndRegion Includes

#Region Main
    _Main_1()
    _Main_2()

    Func _Main_1()
        Local $aArr[4]
        Local $iInx = -1

        $aArr[_Increase($iInx)] = "Index " & $iInx     ; $iInx should be 0 in array, and index in ArrayDisplay should be 0
        $aArr[_Increase($iInx)] = "Index " & $iInx     ; $iInx should be 0 in array, and index in ArrayDisplay should be 0
        _ArrayDisplay($aArr)
    EndFunc   ;==>_Main_1

    Func _Main_2()
        Local $aArr[4]
        Local $iInx = -1, $iInx_Return

        $iInx_Return = _Increase($iInx)     ; Increase value from -1
        ConsoleWrite($iInx_Return & @TAB & $iInx & @CRLF)     ; Both values should be equal
        $aArr[$iInx] = "Index " & $iInx

        $iInx_Return = _Increase($iInx)
        ConsoleWrite($iInx_Return & @TAB & $iInx & @CRLF)     ; Both values should be equal
        $aArr[$iInx] = "Index " & $iInx
        _ArrayDisplay($aArr)
    EndFunc   ;==>_Main_2
#EndRegion Main

#Region Functions
    Func _Increase(ByRef $iInt)
        $iInt += 1
        Return $iInt
    EndFunc   ;==>_Increase
#EndRegion Functions

Can someone explain what I am missing?

Link to comment
Share on other sites

  • Developers

It mostly helps when you also tell us what the problem is or the part you don't find logical ....or you want us to guess?. ;) 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Sure thing 😉

I don't want to resize the array, but increase the index pointer. When I do it like that:

$aArr[_Increase($iInx)] = "Index " & $iInx

The functional _Increase gets called twice if you look at the debug output:

#Region Includes
    #include <Array.au3>
#EndRegion Includes

#Region Main
    _Main_1()

    Func _Main_1()
        Local $aArr[4]
        Local $iInx = -1

        ConsoleWrite($iInx & @CRLF) ; Should be -1
        $aArr[_Increase($iInx)] = "Index " & $iInx     ; $iInx should be 0 in array, and index in ArrayDisplay should be 0
        ConsoleWrite($iInx & @CRLF); Should be 0, but is 1
    EndFunc   ;==>_Main_1

#EndRegion Main

#Region Functions
    Func _Increase(ByRef $iInt, $iLine = @ScriptLineNumber)
        Local Static $S_iCall = -1
        $S_iCall += 1
        ConsoleWrite("Call no. " & $S_iCall & " with value " & $iInt & " from line " & $iLine & @CRLF)
        $iInt += 1
        Return $iInt
    EndFunc   ;==>_Increase
#EndRegion Functions

So I am wondering why the function _Increase gets called twice.

Link to comment
Share on other sites

  • Developers

mmm ... guess it is too late for me to understand why this is happening,but this simple version sums up the issue: The second liune calls the Func 2 times in stead of one time:

Local $iInx = -1
dim $aArr[4]

ConsoleWrite($iInx & @CRLF) ; is -1
_Increase($iInx)            ; calls the func 1 time
ConsoleWrite($iInx & @CRLF) ; is 0
$aArr[_Increase($iInx)] = 1 ; calls the func 2 times
ConsoleWrite($iInx & @CRLF) ; Should be 1, but is 2

Func _Increase(ByRef $iInt, $iLine = @ScriptLineNumber)
     ConsoleWrite("value=" & $iInt & " from line " & $iLine & @CRLF)
    $iInt += 1
    Return $iInt
EndFunc   ;==>_Increase

Change the Dim $aArr[4] to  1, run it and 2 and run it and wonder about the result.... I am going to sleep on it. 

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ahh i see...

It doesn't look like it's a ByRef bug... It also happens with a global variable. or did i miss something?

Global $iint = -1
Dim $aArr[4]

ConsoleWrite($iint & @CRLF) ; is -1
_Increase()            ; calls the func 1 time
ConsoleWrite($iint & @CRLF) ; is 0
$aArr[_Increase()] = 1 ; calls the func 2 times
ConsoleWrite($iint & @CRLF) ; Should be 1, but is 2

Func _Increase()
    $iInt += 1
    ConsoleWrite("value=" & $iInt & @CRLF)
    Return $iInt
EndFunc   ;==>_Increase

 

Edited by Aelc

why do i get garbage when i buy garbage bags? <_<

Link to comment
Share on other sites

I ran your code and could see the problem but thought I would have breakfast before posting a response. On return I see there are several replies. Nonetheless, here is my response;

Now retired, but when working for an AntiVirus company I used AutoIt to develop certain in house tools. I was often frustrated when stuff didn't work that should have worked. It is a wonderful tool but has some idiosyncrasies.

Anyway, I eventually learned that you are playing with fire trying to include functions within expressions. I learned to break my complex expressions down into a series of simplified line by line steps.

The issue here has something to do with AutoIt rules of precedence in evaluating the expression. For some reason, which I am not going to bother to research, your function is being evaluated twice leading to the double increment.

Simplify your code. Don't try anything fancy like placing functions within an expression, particularly as you have done, within the [] referencing an array element.

Also when passing your variable to the function, "byref", any operation within the function acts directly on the variable. There is no point returning the variable with a "Return $iInt" statement as this is redundant.

This works;

Func _Main_1()
  Local $aArr[4]
  Local $iInx = -1
  _Increase($iInx)
  $aArr[$iInx] = "Index " & $iInx           ; $iInx should be 0 in array, and index in ArrayDisplay should be 0
  _Increase($iInx)
  $aArr[$iInx] = "Index " & $iInx           ; $iInx should be 0 in array, and index in ArrayDisplay should be 0
  _ArrayDisplay($aArr)
EndFunc   ;==>_Main_1

 

Edited by pseakins
intro

Phil Seakins

Link to comment
Share on other sites

It is not global nor byref bug, it is an array bug.  Calling a function inside an array assignment calls it twice :

#include <Array.au3>

Local $aArr[4]

$aArr[_Call()] = 1 ; calls the func 2 times
_ArrayDisplay($aArr)

Func _Call()
    ConsoleWrite("called" & @CRLF)
EndFunc

Applies also to 2D arrays...

Edited by Nine
Link to comment
Share on other sites

  • Developers
5 hours ago, Nine said:

It is not global nor byref bug, it is an array bug.  Calling a function inside an array assignment calls it twice :

Correct ...  nothing to do with byref of global  and all could have known when they would have done what I stated! :) 

  

10 hours ago, Jos said:

Change the Dim $aArr[4] to  1, run it and 2 and run it and wonder about the result.... I am going to sleep on it. 

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

This issue seems to be resolved in the current Alpha version, so guess it was already known and worked on..

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

#include <Array.au3>

Local $aArr[4] = ["x", "y", "z", "a"]
Local $sVal = $aArr[_Call()]
ConsoleWrite($sVal & @CRLF)

Func _Call()
    ConsoleWrite("called" & @CRLF)
EndFunc

Seems only to be called twice, when the value of the array is changed.

 

@Jos, good to know. Is there clue, when a new version will be available?

Edited by HurleyShanabarger
Link to comment
Share on other sites

15 minutes ago, HurleyShanabarger said:

Seems only to be called twice, when the value of the array is changed.

And it is not call twice either on declaration :

#include <Array.au3>

Local $aArr[_Call()+1] = [0]
Local $sVal = $aArr[_Call()]
ConsoleWrite($sVal & @CRLF)

$aArr[_Call()] = 1 ; calls the func 2 times
_ArrayDisplay($aArr)

Func _Call()
    ConsoleWrite("called" & @CRLF)
EndFunc

 

Link to comment
Share on other sites

1 hour ago, HurleyShanabarger said:

@Jos, good to know. Is there clue, when a new version will be available?

Take a look on:
https://www.autoitscript.com/trac/autoit/timeline

there is many fixes and changes made by @jpm already sent to @Jon but you must to know that all we here (including @Jon ) we are volunteers.

It will be available when @Jon find spare time to validate the code and release version.

Please do not ask further question like "When ...."

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • Developers
6 hours ago, HurleyShanabarger said:

@Jos, good to know. Is there clue, when a new version will be available?

No and do not bet it will be anytime soon, so just use a workaround for the moment by separating them :

_Increase($iInx) 
$aArr[iInx] = 1

 

5 hours ago, mLipok said:

It will be available when @Jon find spare time to validate the code and release version.

Let's make sure we do not create any expectations here and for now assume it isn't there.
I merely made the statement to indicate that there is no need to submit a bug report as it has been worked on.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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