Page 2 of 3

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 10:33 am
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. 👍

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 11:04 am
by miso
Also maybe Ref?

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 11:15 am
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?

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 11:51 am
by Cezary
So maybe this keyword should be "InOut", "BiDir" or something similar?

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 12:48 pm
by IceSoft
+1 for new keyword 'out'

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 1:18 pm
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.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 1:34 pm
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).

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 2:43 pm
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.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 3:29 pm
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"

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 3:39 pm
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

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 4:08 pm
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.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 4:33 pm
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.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 11:08 pm
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.

Re: Variable by reference in procedure

Posted: Mon May 12, 2025 11:46 pm
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

Re: Variable by reference in procedure

Posted: Tue May 13, 2025 4:35 am
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.