Variable as a reference or value to a function
#19
(08-08-2022, 02:14 PM)Jack Wrote:
(07-18-2022, 03:38 PM)mdijkens Wrote: The reason your initial code passes by value to the sub is the use of double brackets
Call MySub(a) ' passes reference to a
Call MySub((a)) ' evaluates (a) to tempvalue and passes reference to tempvalue

(Same is true for Functions btw)
I don't think that enclosing the argument in parenthesis forces the argument to be treated as by value, 

Try it. It is really true!


Code: (Select All)
x = 3
subproc x
Print "passed byref:"; x ' prints 4

x = 3
subproc (x)
Print "passed byval:"; x ' prints 3

x = 3
Call subproc(x)
Print "passed byref:"; x ' prints 4

x = 3
Call subproc((x))
Print "passed byval:"; x ' prints 3

End

Sub subproc (a)
  a = a + 1
End Sub
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply


Messages In This Thread
RE: Variable as a reference or value to a function - by mdijkens - 08-08-2022, 02:32 PM



Users browsing this thread: 7 Guest(s)