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.
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
I will try again:
Code: (Select All) Dim ALong As Long
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
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 INTEGERNow, 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 _FLOATAnd 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"
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
|
|
« Next Oldest | Next Newest »
|
| Possibly Related Threads… | |||||
| Thread | Author | Replies | Views | Last Post | |
| Function Pointers? | BlameTroi | 5 | 256 |
02-20-2026, 05:55 PM Last Post: BlameTroi |
|
| Variable length type declarations | dano | 5 | 695 |
08-06-2025, 09:53 PM Last Post: dano |
|
| Determining if variable or static string is passed to a Sub | dano | 9 | 1,233 |
06-20-2025, 06:31 PM Last Post: CookieOscar |
|
| Most efficient way to build a big variable length string? | mdijkens | 9 | 1,912 |
01-17-2025, 11:36 PM Last Post: ahenry3068 |
|
| Huge array of variable length strings | mdijkens | 9 | 1,858 |
10-17-2024, 02:01 PM Last Post: mdijkens |
|
Users browsing this thread: 1 Guest(s)


![[Image: QBasic-Referenz-Kl.jpg]](https://i.ibb.co/Bf3ymqb/QBasic-Referenz-Kl.jpg)