Sign in to follow this  
Followers 0

Dont understand ByRef

5 posts in this topic

#1 ·  Posted

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?

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by JohnOne

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#3 ·  Posted

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

Share this post


Link to post
Share on other sites

#4 ·  Posted

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

Visit the SciTE4AutoIt3 Download page for the latest versions                                                                 Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by MilesAhead

"I don't want to belong to any club that would have me as a member."- Groucho Marx

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