Sign in to follow this  
Followers 0
Chimp

doubt about ByRef

11 posts in this topic

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


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

Share this post


Link to post
Share on other sites



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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#3 ·  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

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

PincoPanco,

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

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#6 ·  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.

Share this post


Link to post
Share on other sites

It's quite trivial to test.

But it does not tell if the array is copied internally.

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

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.


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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

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.

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

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


small minds discuss people average minds discuss events great minds discuss ideas.... and use 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
Sign in to follow this  
Followers 0