Variable by reference in procedure

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Kiffi
Addict
Addict
Posts: 1502
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Variable by reference in procedure

Post by Kiffi »

Fred wrote: Mon May 12, 2025 8:44 aman 'out' keyword specific for params which needs to be used as a return value, may be it could be a way to do it.
That sounds very good. 👍
Hygge
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Variable by reference in procedure

Post by miso »

Also maybe Ref?
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Variable by reference in procedure

Post by Little John »

Fred wrote: 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.
By reference means, that values can be passed in and out. If the 'Out' keyword in PureBasic would have the same effect, then its name would be confusing. Or is the 'Out' keyword in PureBasic supposed to allow only output of values?
Cezary
Enthusiast
Enthusiast
Posts: 109
Joined: Sun Feb 12, 2017 2:31 pm

Re: Variable by reference in procedure

Post by Cezary »

So maybe this keyword should be "InOut", "BiDir" or something similar?
User avatar
IceSoft
Addict
Addict
Posts: 1694
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Variable by reference in procedure

Post by IceSoft »

+1 for new keyword 'out'
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Cezary
Enthusiast
Enthusiast
Posts: 109
Joined: Sun Feb 12, 2017 2:31 pm

Re: Variable by reference in procedure

Post by Cezary »

Instead of "ByRef" it is also possible to use "Ptr" for simple variables. For structures it could also be possible (optionally) to use "Ptr", e.g.: "Ptr abc.MyStruc", then the syntax would remain clear.
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 »

Little John wrote: Mon May 12, 2025 11:15 am
Fred wrote: 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.
By reference means, that values can be passed in and out. If the 'Out' keyword in PureBasic would have the same effect, then its name would be confusing. Or is the 'Out' keyword in PureBasic supposed to allow only output of values?
With out keyword there will be no input value (string will be always empty).
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 »

If we have pointers and structures, what problem is being solved with an 'out' or 'in' keyword?

Is this to speed up strings?
Then enable the PB string functions to accept memory locations and sizes instead of string variables.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
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 »

Fred wrote: Mon May 12, 2025 1:34 pmWith out keyword there will be no input value (string will be always empty).
I think it's better to pass the variable by reference (input and output).

Code: Select all

Procedure test(Ref s.s)
  s + " World"
EndProcedure

s.s="Hello"
Debug s ; "Hello"
test(s)
Debug s ; "Hello World"
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Variable by reference in procedure

Post by mk-soft »

There is only the problem with strings to pass them as ByRef, because you will not have the address of the string variable where the pointer to the string is entered.
This means that the string cannot be changed.

P.S.
Passing the string variable ByRef would also have the advantage that a copy of the string would not always be created in the procedure first.

Code: Select all

Procedure foo(ByRef text.s)
  ;
EndProcedure
P.S.
Same features request from 2019
Link: VarPtr for PureBasic
Last edited by mk-soft on Mon May 12, 2025 4:12 pm, edited 1 time in total.
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 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 »

So this is only for strings?
I would prefer a faster String lib than introducing a new keyword.
If we must have a new keyword, then ByRef is the traditional term used in Basics.
ByVal or copy is the default unless ByRef is assigned.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
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 »

skywalk wrote: Mon May 12, 2025 4:08 pmSo this is only for strings?
Better for all types including structures. If there is a type mismatch (for example, in a procedure the type expected is "Integer", but the type passed is "Byte"), the compiler reports an error.
User avatar
idle
Always Here
Always Here
Posts: 5896
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Variable by reference in procedure

Post by idle »

for strings wouldn't it be easier to do it like c with **myStr
The problem as I see it is when a string resides in the datasection, you can modify it but your can't reallocate it
currently to get the address of a string you need to cast it as *ptr.string = @*myStr from the parameter *myStr which is the same as **mystr in c
or maybe better just add structure returns to procedures so we can return structures which would solve the problem in a cleaner way.
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 »

idle wrote: Mon May 12, 2025 11:08 pm or maybe better just add structure returns to procedures so we can return structures which would solve the problem in a cleaner way.
+1000000. I've wanted this feature in PB since ForEver :D
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Variable by reference in procedure

Post by AZJIO »

The meaning of "ByRef" is not to copy the string. If the function is run in a loop, copying data may slow down data processing. The behavior of "Out" is unclear. If a string is copied and then inserted into a variable with the keyword "Out", then the task of data output is solved, rather than working with the original string. If we are working with the original string, we will not need to output it to a variable, since it will change anyway.
Post Reply