Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Dont understand ByRef


  • Please log in to reply
4 replies to this topic

#1 kor

kor

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 570 posts

Posted 16 March 2012 - 10:21 PM

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?







#2 JohnOne

JohnOne

    John

  • Active Members
  • PipPipPipPipPipPip
  • 12,897 posts

Posted 16 March 2012 - 10:33 PM

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, 16 March 2012 - 10:35 PM.

AutoIt Absolute Beginners Require a serial
 
 
OMG! Women are the cows of people.


#3 kor

kor

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 570 posts

Posted 16 March 2012 - 10:36 PM

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


#4 Jos

Jos

    Je maintiendrai

  • Developers
  • 23,355 posts

Posted 16 March 2012 - 10:40 PM

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.
  :)


#5 MilesAhead

MilesAhead

    Eclectician

  • Active Members
  • PipPipPipPipPipPip
  • 607 posts

Posted 17 March 2012 - 02:31 AM

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, 17 March 2012 - 02:35 AM.

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users