BYVAL in the wiki jumps to DECLARE LIBRARY - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +---- Forum: Wiki Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=25) +---- Thread: BYVAL in the wiki jumps to DECLARE LIBRARY (/showthread.php?tid=2224) |
BYVAL in the wiki jumps to DECLARE LIBRARY - bobalooie - 12-04-2023 I noticed this today. I reckon the wiki isn't supposed to do that? RE: BYVAL in the wiki jumps to DECLARE LIBRARY - mnrvovrfc - 12-04-2023 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". RE: BYVAL in the wiki jumps to DECLARE LIBRARY - bobalooie - 12-04-2023 (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? RE: BYVAL in the wiki jumps to DECLARE LIBRARY - FellippeHeitor - 12-05-2023 Wrap variables in parenthesis when passing to a sub. That makes it go byval rather than byref. RE: BYVAL in the wiki jumps to DECLARE LIBRARY - SMcNeill - 12-05-2023 (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) With the above, you'll never pass values back and forth, so you never have to worry about corrupting your original variables. |