Variable by reference in procedure

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User_Russian
Addict
Addict
Posts: 1535
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Variable by reference in procedure

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Variable by reference in procedure

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Variable by reference in procedure

Post 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
User_Russian
Addict
Addict
Posts: 1535
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Variable by reference in procedure

Post 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
User_Russian
Addict
Addict
Posts: 1535
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Variable by reference in procedure

Post 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$
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Variable by reference in procedure

Post 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]
User_Russian
Addict
Addict
Posts: 1535
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Variable by reference in procedure

Post 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 
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Variable by reference in procedure

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User_Russian
Addict
Addict
Posts: 1535
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Variable by reference in procedure

Post 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
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Variable by reference in procedure

Post by skywalk »

Or just use structured variables.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Variable by reference in procedure

Post 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.
Cezary
Enthusiast
Enthusiast
Posts: 109
Joined: Sun Feb 12, 2017 2:31 pm

Re: Variable by reference in procedure

Post 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.
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Variable by reference in procedure

Post 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.
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Variable by reference in procedure

Post by miso »

+1 on Out
Last edited by miso on Mon May 12, 2025 10:06 am, edited 1 time in total.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Variable by reference in procedure

Post by Caronte3D »

+1000 from me on the Out keyword.
Post Reply