Page 1 of 3

Variable by reference in procedure

Posted: Sun May 11, 2025 9:08 pm
by User_Russian
It's strange that there is still no support for passing variables by reference in procedure parameters. For example, you need to return several string variables from a procedure, but this is not supported! The only thing I managed to do was this code, but it’s not safe!

Code: Select all

Procedure test(*s)
  Protected s.string
  PokeI(@s, *s)
  s\s = "5678"
  PokeI(@s, 0)
EndProcedure

s.s="1234"
test(@s)
Debug s

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 9:18 pm
by jacdelad
If you pass the address of a variable, what's the difference to passing it by reference? I don't see any.
Also if you have to return multiple values, pass a structure.

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 9:20 pm
by AZJIO
It would be great to have that option

Code: Select all

Procedure test(String s)
jacdelad wrote: Sun May 11, 2025 9:18 pm Also if you have to return multiple values, pass a structure.
It is not convenient to create a structure in order to pass it by reference.

At the moment, there is a restriction not to increase the row, here is an example

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 10:06 pm
by User_Russian
jacdelad wrote: Sun May 11, 2025 9:18 pmIf you pass the address of a variable, what's the difference to passing it by reference?
This does not work correctly for string variables.

Code: Select all

Procedure test(*s.String)
  *s\s = "5678"
EndProcedure

s.s="1234"
test(@s)
Debug s

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 10:36 pm
by User_Russian
It turned out that this code does not work correctly.

Code: Select all

Procedure test(*s)
  Protected s.string
  PokeI(@s, *s)
  s\s = "5678"+Space(1000000)
  PokeI(@s, 0)
EndProcedure

s.s="1234"
test(@s)
Debug s

Code: Select all

Procedure.s TrimLeft(*a, n)
  Protected *p.string = @*a
  *p\s = Right(*p\s, Len(*p\s) - n)+Space(1000000)
EndProcedure
tmp$ = "Hello world"
TrimLeft(@tmp$, 3)
Debug tmp$

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 10:56 pm
by idle
use pokeS or @* to a string pointer

Code: Select all

Procedure test(*s.String)
  PokeS(*s,"5678")
EndProcedure

Procedure test1(*s)
  Protected *ss.String 
  *ss = @*s 
  *ss\s = "1234"
EndProcedure

s.s = "1234"
test(@s)
Debug s
test1(@s)
Debug s 

[/code]

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 11:03 pm
by User_Russian
idle, in your code has a similar problem.

Code: Select all

Procedure test(*s.String)
  PokeS(*s,"5678")
EndProcedure

Procedure test1(*s)
  Protected *ss.String 
  *ss = @*s 
  *ss\s = "Space "+Space(1000000)
EndProcedure

s.s = "1234"
test(@s)
Debug s
test1(@s)
Debug s 

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 11:08 pm
by mk-soft
None of this works. You have to pass the pointer to pointer to the string.
That's why I wanted the VarPtr function to get the var pointer to the string.

It only works like this

Code: Select all

Procedure foo(*s.String)
  
  *s\s = "Hello"
  
EndProcedure

Define sVar.String

foo(@sVar)
Debug sVar\s

Re: Variable by reference in procedure

Posted: Sun May 11, 2025 11:14 pm
by User_Russian
Yes it works, but it requires copying the strings into a String structure, because the code contains regular strings, not String structures. This is extra code and time to copy the string.

Code: Select all

Procedure foo(*s.String)
  *s\s = "Hello"
EndProcedure

Define s.s="1234", sVar.String\s = s

foo(@sVar)

s = sVar\s
Debug s

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 7:02 am
by skywalk
Or just use structured variables.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 8:44 am
by Fred
I don't like the byref vs byval stuff in a BASIC language, it's very confusing. In C# you have a an 'out' keyword specific for params which needs to be used as a return value, may be it could be a way to do it.

Code: Select all

Procedure GetMinMaxStrings(Input$, Out String1$, Out String2$)
   ...
EndProcedure
The compiler can check than the 2 'out' variables have been really assigned before exiting the procedure.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 9:01 am
by Cezary
I've also needed a procedure to return more than one value many times. I once added the ability to return two values ​​to my wish list (for which I was immediately criticized). The solution with the word 'Out' is good in my opinion and I join the request.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 9:18 am
by Quin
Agreed about byref and byval being confusing in BASICs, especially because some people use byval everywhere... :twisted:
+1000 from me on the Out keyword.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 9:35 am
by miso
+1 on Out

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 9:50 am
by Caronte3D
+1000 from me on the Out keyword.