Variable as a reference or value to a function - 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) +--- Thread: Variable as a reference or value to a function (/showthread.php?tid=628) |
RE: Variable as a reference or value to a function - Kernelpanic - 07-18-2022 From: Microsoft QuickBasic 4 - Programmieren in Basic - Ausgewählte Themen RE: Variable as a reference or value to a function - bplus - 07-18-2022 @KernelPanic, Something is lost in translation. It would help to show code and not screen shots. Also it would help if you quit assuming we can read German. Also while QB64 tries to be as compatible as possible to old QB's there are places where it is not. And default pass by reference is one of those areas, so don't quote old QB reference (which did pass by value as default), which is out-of-date. Quote from current QB64 Wiki where you are finding discrepancy. RE: Variable as a reference or value to a function - bplus - 07-18-2022 I will try again: Code: (Select All) Dim ALong As Long RE: Variable as a reference or value to a function - SMcNeill - 07-18-2022 Functions will pass by reference in the exact same manner as subs. The main rule of thumb is that variable types *must* match to pass. Examples: Code: (Select All) DIM x AS INTEGER Now, the above will print 2, 4, 6 onto the screen, as the value of X changes and is passed back to the main routine with each run of the function. Code: (Select All) DIM x AS _FLOAT And the above will print 2, 2, 2 on the screen, as the function Foo uses an Integer, and the main module is passing a _FLOAT. The two variable types aren't the same, so the value won't pass by reference. For SUBs and for FUNCTIONs, variables pass by reference as long as the types match. RE: Variable as a reference or value to a function - mdijkens - 07-18-2022 The reason your initial code passes by value to the sub is the use of double brackets Call MySub(a) ' passes reference to a Call MySub((a)) ' evaluates (a) to tempvalue and passes reference to tempvalue (Same is true for Functions btw) btw i prefer to write (and read): MySub a 'byref MySub (a) 'byval RE: Variable as a reference or value to a function - Kernelpanic - 07-18-2022 Ok, I think I have understood it now. This tip from Steve was crucial (Thanks!): For SUBs and for FUNCTIONs, variables pass by reference as long as the types match. The variable must be explicitly changed in the function. AsValue = input * 3 -- this doesn't work. This doesn't work even if the variable types match. Same goes for C. I've been trying to change the value at the address of the variable for over two hours. Now it worked. Code: (Select All) 'Uebergabe von Variablen an Funktionen als Referenz oder Wert. RE: Variable as a reference or value to a function - TempodiBasic - 07-24-2022 Hi friends about this thumb up rule showed at #13 by Steve We must admit that it is ok but just different from QBasic. In Qbasic , for using FUNCTION and SUB, it needs DECLAREtion of prototype of SUB or FUNCTION at the beginning of the code file. So if you use a different type of data in passing parameter you got an Error for Data Mysmatch on running the code. See next time Good Coding RE: Variable as a reference or value to a function - Jack - 08-08-2022 (07-18-2022, 03:38 PM)mdijkens Wrote: The reason your initial code passes by value to the sub is the use of double bracketsI don't think that enclosing the argument in parenthesis forces the argument to be treated as by value, at least not in the code I am trying to debug honestly, why in the world doesn't QB64 have byval and byref for arguments to sub's and functions? in another thread I posted a portion of Treebeards string math routines, the basic 4 functions seem to work but the Sqr function and the trig functions don't work, tried it in VB.net and at first I was getting no result until I changed the parameters that I could guess were meant to be modified to byref, then all the functions worked including the Sqr, logs and trigs RE: Variable as a reference or value to a function - mdijkens - 08-08-2022 (08-08-2022, 02:14 PM)Jack Wrote:(07-18-2022, 03:38 PM)mdijkens Wrote: The reason your initial code passes by value to the sub is the use of double bracketsI don't think that enclosing the argument in parenthesis forces the argument to be treated as by value, Try it. It is really true! Code: (Select All) x = 3 RE: Variable as a reference or value to a function - bplus - 08-08-2022 Does not work for strings Code: (Select All) x$ = "3" |