In Steve's Posting about the Wiki being incorrect for FUNCTION
https://www.qb64.org/forum/index.php?topic=1531.0I pointed out that arguments passed to a FUNCTION are passed By Reference by default and not by By Value. That is to say, the argument gets modified in the FUNCTION and that modified value gets passed back to the calling argument. This was a surprise to me, not to say rather a shock.
Incidentally, I never can remember which is By Reference and which is By Value (always have to look them up) – I'd rather they were called CHANGED and UNCHANGED respectively.
So, I discovered that if you don't want a FUNCTION to change your calling argument, you must pass the argument By Value, and this is done (QB64 methodology) by placing the argument within brackets, viz. if a FUNCTION F%() is called using a calling argument a% then
b% = F%(a%) - By Reference: a% will be modified
b% = F%((a%)) - By Value: a% will remain unchanged
So, I thought that I'd got that understood until I tried passing a String to a FUNCTION. In this case, the String is always passed By Reference (and gets modified), no matter whether it is inside brackets or not. Then I found that exactly the same thing happens with a SUB – a String is always modified by a SUB whether or not it is inside brackets. I hope that you're still with me on this!
'Function / Sub By Val or By Ref
'Now, the authentic definition is:
'When a parameter is passed by reference, the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller's variable.
'When a parameter is passed by value, the caller and callee have two independent variables with the same value. If the callee modifies the parameter variable, the effect is not visible to the caller.
C1% = 3
PRINT C1%;
"- Numeric Original" PRINT C1%;
"- Numeric After SUB By Value" PRINT Ch%
((C1%
));
"- FUNCTION Result After FUNCTION By Value" PRINT C1%;
"- Numeric After FUNCTION By Value" PRINT Ch%
(C1%
);
"- FUNCTION Result After FUNCTION By Reference" PRINT C1%;
"- Numeric After FUNCTION By Reference" PRINT C1%;
"- Numeric After SUB By Reference"
W2$ = "Change[to[Spaces"
PRINT W2$;
" - String Original" PRINT W2$;
" - String After SUB By Value" PRINT Spaced$
((W2$
));
" - FUNCTION Result After FUNCTION By Value" PRINT W2$;
" - String After FUNCTION By Value" PRINT Spaced$
(W2$
);
" - FUNCTION Result After FUNCTION By Reference" PRINT W2$;
" - String Result After FUNCTION By Reference" PRINT W2$;
" - String Result After SUB By Reference"
C% = C% + 1
Ch% = 2 * C%
Spaced$ = X$
C% = 3 * C%
X$ = X$ + "[zzz"
In the given code, there is a FUNCTION Ch%() which takes an Integer argument and a FUNCTION Spaced$() which takes a String argument, and there is a SUB Cg() which takes an Integer argument and a SUB Spacef() which takes a String argument. The main block calls the FUNCTIONs and SUBs By Value and By Reference.
The code demonstrates what I have declared here: a Numeric may be passed to a SUB or to a FUNCTION either By Reference (default) or By Value, but a String is always passed to a SUB or to a FUNCTION By Reference.
Is this supposed to happen, and if so should a String behave differently to a Numeric?
From a queasy Qwerkey.