That sounds very good.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.
Variable by reference in procedure
Re: Variable by reference in procedure
Hygge
Re: Variable by reference in procedure
Also maybe Ref?
-
- Addict
- Posts: 4789
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Variable by reference in 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?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.
The compiler can check than the 2 'out' variables have been really assigned before exiting the procedure.Code: Select all
Procedure GetMinMaxStrings(Input$, Out String1$, Out String2$) ... EndProcedure
Re: Variable by reference in procedure
So maybe this keyword should be "InOut", "BiDir" or something similar?
Re: Variable by reference in procedure
+1 for new keyword 'out'
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Re: Variable by reference in procedure
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
With out keyword there will be no input value (string will be always empty).Little John wrote: Mon May 12, 2025 11:15 amBy 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?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.
The compiler can check than the 2 'out' variables have been really assigned before exiting the procedure.Code: Select all
Procedure GetMinMaxStrings(Input$, Out String1$, Out String2$) ... EndProcedure
Re: Variable by reference in procedure
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.
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
-
- Addict
- Posts: 1535
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: Variable by reference in procedure
I think it's better to pass the variable by reference (input and output).Fred wrote: Mon May 12, 2025 1:34 pmWith out keyword there will be no input value (string will be always empty).
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
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.
P.S.
Same features request from 2019
Link: VarPtr for PureBasic
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
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Variable by reference in procedure
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.
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
-
- Addict
- Posts: 1535
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: Variable by reference in procedure
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
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.
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
+1000000. I've wanted this feature in PB since ForEveridle 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.

Re: Variable by reference in procedure
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.