Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User_Russian
Addict
Posts: 1535 Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia
Post
by User_Russian » Sun May 11, 2025 9:08 pm
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
jacdelad
Addict
Posts: 2010 Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa
Post
by jacdelad » Sun May 11, 2025 9:18 pm
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
Posts: 2191 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Sun May 11, 2025 9:20 pm
It would be great to have that option
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
Posts: 1535 Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia
Post
by User_Russian » Sun May 11, 2025 10:06 pm
jacdelad wrote: Sun May 11, 2025 9:18 pm If 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
Posts: 1535 Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia
Post
by User_Russian » Sun May 11, 2025 10:36 pm
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$
idle
Always Here
Posts: 5897 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sun May 11, 2025 10:56 pm
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
Posts: 1535 Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia
Post
by User_Russian » Sun May 11, 2025 11:03 pm
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
mk-soft
Always Here
Posts: 6246 Joined: Fri May 12, 2006 6:51 pm
Location: Germany
Post
by mk-soft » Sun May 11, 2025 11:08 pm
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
User_Russian
Addict
Posts: 1535 Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia
Post
by User_Russian » Sun May 11, 2025 11:14 pm
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
skywalk
Addict
Posts: 4218 Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA
Post
by skywalk » Mon May 12, 2025 7:02 am
Or just use structured variables.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Posts: 18220 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Mon May 12, 2025 8:44 am
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
Posts: 109 Joined: Sun Feb 12, 2017 2:31 pm
Post
by Cezary » Mon May 12, 2025 9:01 am
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
Posts: 1133 Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:
Post
by Quin » Mon May 12, 2025 9:18 am
Agreed about byref and byval being confusing in BASICs, especially because some people use byval everywhere...
+1000 from me on the Out keyword.
miso
Enthusiast
Posts: 466 Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary
Post
by miso » Mon May 12, 2025 9:35 am
+1 on Out
Last edited by
miso on Mon May 12, 2025 10:06 am, edited 1 time in total.
Caronte3D
Addict
Posts: 1361 Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe
Post
by Caronte3D » Mon May 12, 2025 9:50 am
+1000 from me on the Out keyword.