Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BYVAL in the wiki jumps to DECLARE LIBRARY
#1
I noticed this today. I reckon the wiki isn't supposed to do that?
It's not the having, it's the doing.
Reply
#2
It's because the only use of BYVAL is within DECLARE LIBRARY block. It cannot be used with "regular" user subprograms. In other words, when you write a SUB or FUNCTION which doesn't depend on any external library, all parameters without exception are passed by reference. Freebasic has the option to pass parameter by value or by reference, also has "BYREF" but it's more confusing and it's better there to use pointers, which has to be done in Pure Basic as well to change fields part of a structure variable being passed to a "PROCEDURE".
Reply
#3
(12-04-2023, 07:29 PM)mnrvovrfc Wrote: It's because the only use of BYVAL is within DECLARE LIBRARY block. It cannot be used with "regular" user subprograms. In other words, when you write a SUB or FUNCTION which doesn't depend on any external library, all parameters without exception are passed by reference. Freebasic has the option to pass parameter by value or by reference, also has "BYREF" but it's more confusing and it's better there to use pointers, which has to be done in Pure Basic as well to change fields part of a structure variable being passed to a "PROCEDURE".
Cool, thanks! So, if I want to duplicate the classic BYVAL behavior, I would need to declare a local version of the passed variables and copy the values over, correct?
It's not the having, it's the doing.
Reply
#4
Wrap variables in parenthesis when passing to a sub. That makes it go byval rather than byref.
Reply
#5
(12-05-2023, 01:35 PM)FellippeHeitor Wrote: Wrap variables in parenthesis when passing to a sub. That makes it go byval rather than byref.

Just be warned -- there are a few instances where doing this will cause issues with your program.  (I think it's with strings, or arrays of strings, or some such similar which errors out and doesn't work, and we haven't came up with a fix for the issue yet.)

The best practice, if you know you're always going to be passing into a sub BYVAL, is just to pass to temp variables first:

Code: (Select All)
SUB foo (tempX, tempY)
    x = tempX
    y = tempY

With the above, you'll never pass values back and forth, so you never have to worry about corrupting your original variables.
Reply




Users browsing this thread: 1 Guest(s)