kor Posted March 16, 2012 Share Posted March 16, 2012 I have 2 functions and 2 arrays. Arrays X and Y are manipulated in Function 1. I need to pass the entire arrays of X and Y over to function 2 and then manipulate them more in function 2. I'm searching and it looks like to accomplish this I use ByRef but I don't understand the usage. Func _1() Dim $X = ["1", "2", "3"] EndFunc ;==>_1 Func _2() For $i = 1 To UBound($X) - 1 ConsoleWrite($X[$i] & " test" & @CRLF) Next EndFunc How do I get the array $X over into function 2? Link to comment Share on other sites More sharing options...
JohnOne Posted March 16, 2012 Share Posted March 16, 2012 (edited) Func _1() Dim $X = ["1", "2", "3"] _2($x) EndFunc ;==>_1 Func _2(ByRef $array) For $i = 1 To UBound($array) - 1 ConsoleWrite($array[$i] & " test" & @CRLF) Next EndFunc Note, that $x, while created in _1() will be modified by _2() Edited March 16, 2012 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...
kor Posted March 16, 2012 Author Share Posted March 16, 2012 John, what do you think of this. _1() _2($X) Func _1() Global $X[3] = ["1", "2", "3"] EndFunc ;==>_1 Func _2(ByRef $X) For $i = 1 To UBound($X) - 1 ConsoleWrite($X[$i] & " test" & @CRLF) Next EndFunc Link to comment Share on other sites More sharing options...
Developers Jos Posted March 16, 2012 Developers Share Posted March 16, 2012 This is a simple example showing the difference with and without byref: Dim $X[3] = ["1", "2", "3"] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $array[0] = ' & $x[0] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console _1($x) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $array[0] = ' & $x[0] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console _2($x) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $array[0] = ' & $x[0] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console ; Func _1( $array) $array[0]=0 EndFunc ; Func _2(ByRef $array) $array[0]=0 EndFunc 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 More sharing options...
MilesAhead Posted March 17, 2012 Share Posted March 17, 2012 (edited) John, what do you think of this. _1() _2($X) Func _1() Global $X[3] = ["1", "2", "3"] EndFunc ;==>_1 Func _2(ByRef $X) For $i = 1 To UBound($X) - 1 ConsoleWrite($X[$i] & " test" & @CRLF) Next EndFunc In JohnOne's version, one(no pun intended) should only call func _2 from func _1 so that the array variable will exist(perfectly valid as long as one knows the limitation.) Otoh, the way you implemented it, there's no real need for ByRef since it can be accessed globally directly. ByRef is basically syntactic sugar that allows you to use the variable without having to bother with stuff like pointer dereferencing. Esp. with arrays dereferincing the pointer then using the bracket operator '[]' can get complicated. Much neater to just do it as if the variable was global(at least afa as syntax goes.) Just think of it as the interpreter/compiler dereferencing the pointer for you under the covers. (I'm talking languages where pointers are a data type supported by the language intrinsically.. such as C/C++ Pascal etc.. In effect, if it was Pascal a ByRef is the same as a Var parameter. Edited March 17, 2012 by MilesAhead My Freeware Page 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