Gianni Posted October 13, 2013 Share Posted October 13, 2013 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 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
FireFox Posted October 13, 2013 Share Posted October 13, 2013 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. Link to comment Share on other sites More sharing options...
Gianni Posted October 13, 2013 Author Share Posted October 13, 2013 (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 October 13, 2013 by PincoPanco Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
JohnOne Posted October 13, 2013 Share Posted October 13, 2013 Arrays are passed by reference with or without ByRef keyword. If the function it is passed to wants/attempts to alter the array, it will be copied unless you specify ByRef Keyword. jaberwacky 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
FireFox Posted October 13, 2013 Share Posted October 13, 2013 (edited) PincoPanco,I meant in the case you alter it like JohnOne said. Edited October 13, 2013 by FireFox Link to comment Share on other sites More sharing options...
JohnOne Posted October 13, 2013 Share Posted October 13, 2013 (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 October 13, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
FireFox Posted October 13, 2013 Share Posted October 13, 2013 It's quite trivial to test.But it does not tell if the array is copied internally. Link to comment Share on other sites More sharing options...
JohnOne Posted October 13, 2013 Share Posted October 13, 2013 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. Link to comment Share on other sites More sharing options...
FireFox Posted October 13, 2013 Share Posted October 13, 2013 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. Link to comment Share on other sites More sharing options...
JohnOne Posted October 13, 2013 Share Posted October 13, 2013 I certainly did, and the modifications of it are not reflected in the original array, showing that a copy operation was carried out. And we're full circle. Over and out. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Gianni Posted October 13, 2013 Author Share Posted October 13, 2013 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 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now