But now it does work with CALL and () as well?!?! curious indeed!
Title: Re: simple SUB-procedure doesn't work
Post by: FellippeHeitor on February 22, 2018, 10:09:09 am
@MrFreyer:
You can only change a parameter if it matches types. In your case subsum1 is an INTEGER, but when you create the SUB, subsum1 is a SINGLE precision parameter.
Title: Re: simple SUB-procedure doesn't work
Post by: MrFreyer on February 22, 2018, 10:18:49 am
Okay, now I get it.
Yes, I could do this as a function, but I was testing something other and just posted this selected piece of code.
Thanks!
Title: Re: simple SUB-procedure doesn't work
Post by: bplus on February 22, 2018, 10:28:58 am
OK he gets it... but now I am confused.
subsum1 was dimensioned as an integer!
and code does work with DEFINT A-Z
Not that I would actually do this crazy stuff in code, I can find my own just fine!
Title: Re: simple SUB-procedure doesn't work
Post by: MrFreyer on February 22, 2018, 10:40:37 am
The SUB calc redimensioned the subsum1 as SINGLE (for the use in the subprocedure). But if you use DEFINT A-Z, the subsum1 will automatically redimensioned as a INTEGER .
The variables subvar1, subvar2 and subvar3 will also redimensined as SINGLE, but they don't need to be shared with the maincode. And that's the problem. subsum1 will be shared with the maincode, but a SINGLE (subsum1) and a INTEGER (sum1) dosn't fit together.
Am I right?
Title: Re: simple SUB-procedure doesn't work
Post by: FellippeHeitor on February 22, 2018, 10:40:57 am
Scope is the question here. When you DIM SHARED subvar1 AS INTEGER, you make it available globally. If you create any sub or function, you can access subvar1 from there, change it, erase it, redimension it (if it's an array). However, if you instruct QB64 that a SUB/FUNCTION needs to have a parameter that's also named subvar1, QB64 will assume that for this new scope you want a subvar1 that's not an INTEGER (remember that not defining a type makes a variable of type SINGLE). The local subvar1 parameter takes precedence over the shared variable. They are separate entities.
Internally (in the C++ output) you'll have __INTEGER_SUBVAR1 and _SUB_CALC_SINGLE_SUBVAR1, which can be verified by looking in internal/temp/main.txt.
Title: Re: simple SUB-procedure doesn't work
Post by: Pete on February 22, 2018, 12:23:48 pm
A long time ago in an IDE far far away, we used COMMON SHARED instead of DIM SHARED.
Pete :)
Title: Re: simple SUB-procedure doesn't work
Post by: Edster on February 24, 2018, 07:05:21 am
I have a similar question so I thought I would tack it to the end of this thread.
DIM reply(i) AS STRING CALL Answer(reply()) . . . SUB Answer(passedReply() AS STRING) PRINT passedReply(i) END SUB
The above code works just fine ...
DIM reply(i) AS STRING PRINT Answer(reply()) . . . FUNCTION Answer(passedReply() AS STRING) Answer = passedReply(i) END FUNCTION
This code produces IILEGAL STRING-NUMBER CONVERSION error
However, if 'DEFSTR A' is used, then the above function works fine. The function name needs to be defined as string.
Should FUNCTIONS and SUBS differ in this way? It took me some time to figure out why the FUNCTION wouldn't work.
Title: Re: simple SUB-procedure doesn't work
Post by: FellippeHeitor on February 24, 2018, 07:32:29 am
Now that could be a bug. Give us some time to look into to it.
Edit: No, not a bug. You're trying to return a string but your function is of type SINGLE. Fix it like this: