Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Function Pointers? BlameTroi 5 258 02-20-2026, 05:55 PM
Last Post: BlameTroi
  Variable length type declarations dano 5 695 08-06-2025, 09:53 PM
Last Post: dano
  Determining if variable or static string is passed to a Sub dano 9 1,233 06-20-2025, 06:31 PM
Last Post: CookieOscar
  Most efficient way to build a big variable length string? mdijkens 9 1,916 01-17-2025, 11:36 PM
Last Post: ahenry3068
  Huge array of variable length strings mdijkens 9 1,858 10-17-2024, 02:01 PM
Last Post: mdijkens

Forum Jump:


Users browsing this thread: 1 Guest(s)