Jump to content

Recommended Posts

Posted

If I pass a variable (an array) to a function that accept parameters ByRef and then, in turn, I'll pass the same variable (that is now a reference) to another function that accept parameters without ByRef, is the original array "moved" to the nested function or is it still a refernce?
(the second-level function will have to modify the array)

for example in this script the $arry array in line 19 (that is a reference) when arrive to line 22 is still a reference or will be again transformed in the original array and then "moved" to line 22?

#include <array.au3>
Local $MyArray[3] = ["hours", "minutes", "seconds"]
Local $MyString = "What time is it?"

Display( Myfunc($MyString))
Display( Myfunc("My constant"))
Display( MyfuncArray($MyArray))

Func MyfuncArray(ByRef $arry)
    Return Myfunc($arry) ; >----+   <-- Does this pass a reference of $arry
EndFunc   ;==>MyfuncArray       |       or the whole Array is passed ??
;                               |
Func Myfunc($arry) ;    <-------+
    If IsArray($arry) Then
        $arry[0] = @HOUR
        $arry[1] = @MIN
        $arry[2] = @SEC
    Else
        $arry = "time is " & @HOUR & ":" & @MIN & ":" & @SEC
    EndIf
    Return $arry
EndFunc   ;==>Myfunc

Func Display($returned)
    If IsArray($returned) Then
        _ArrayDisplay($returned)
    Else
        MsgBox(0, "", $returned)
    EndIf
EndFunc   ;==>Display

thanks for any clarification

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted

Hi,

The arrays are copy-on-write, meaning that it will be a reference (with or without a ByRef) until you edit it, in this last case you will need to pass it by reference.

Br, FireFox.

Posted (edited)

Thanks FireFox for reply
it isn't still clear to me
therefore, pass an array ByRef or not, it makes no difference if the array must be modified by the function?
if so, why did you say "in this last case you will need to pass it by reference"?

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted (edited)

#include <Array.au3>

Global $GlobalArray[2] = [0,0]

_AlterGlobalArrayByRef($GlobalArray)

Func _AlterGlobalArrayByRef(ByRef $Array)
    $Array[0] = 1
    _ArrayDisplay($GlobalArray, "_AlterGlobalArrayByRef")
    _AlterPassedArray($Array)
    _AlterPassedArrayByRef($Array)
EndFunc

Func _AlterPassedArray($Array)
    $Array[1] = 1
    _ArrayDisplay($GlobalArray, "_AlterPassedArray")
EndFunc

Func _AlterPassedArrayByRef(ByRef $Array)
    $Array[1] = 1
    _ArrayDisplay($GlobalArray, "_AlterPassedArrayByRef")
EndFunc

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Posted

If it were copied, would the function not need to return it in order for the Original array to be modified?

I believe so, showing that when the ByRef keyword is used, there is no copy operation performed.

It's the whole point of it I think.

Yeah right. Did not see the function without the ByRef.
Posted

PincoPanco,

I meant in the case you alter it like JohnOne said.

@FireFox

I had not read your post well, reading it better then it was clear! (my fault)

thanks!

#include <Array.au3>

Global $GlobalArray[2] = [0,0]

_AlterGlobalArrayByRef($GlobalArray)

Func _AlterGlobalArrayByRef(ByRef $Array)
    $Array[0] = 1
    _ArrayDisplay($GlobalArray, "_AlterGlobalArrayByRef")
    _AlterPassedArray($Array)
    _AlterPassedArrayByRef($Array)
EndFunc

Func _AlterPassedArray($Array)
    $Array[1] = 1
    _ArrayDisplay($GlobalArray, "_AlterPassedArray")
EndFunc

Func _AlterPassedArrayByRef(ByRef $Array)
    $Array[1] = 1
    _ArrayDisplay($GlobalArray, "_AlterPassedArrayByRef")
EndFunc

@JohnOne

well, your test has removed all my doubts!

thanks

my problem is that if a function accept a parameter ByRef, then  it does not accept constants.

my (clumsy) attempt was to pass byref to a function that has not the byref parameter, by using a sort of a secondary entry point used only for ByRef, so to be able to send either variables (byref or not)  than constants to the same function,

now is clear that I have to use two separate functions, one for working variables ByRef and another for working constants and variables not ByRef.

Thanks again

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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
  • Recently Browsing   0 members

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