07-18-2022, 12:45 PM
Variable as a reference or value to a function
|
@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.
b = b + ...
I will try again:
Code: (Select All) Dim ALong As Long
b = b + ...
07-18-2022, 02:39 PM
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.
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
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
07-18-2022, 08:03 PM
(This post was last modified: 07-18-2022, 08:05 PM by Kernelpanic.)
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.
07-24-2022, 02:35 PM
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
08-08-2022, 02:14 PM
(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 (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
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Does not work for strings
Code: (Select All) x$ = "3"
b = b + ...
|
« Next Oldest | Next Newest »
|
Users browsing this thread: 9 Guest(s)