QB64Team / qb64

BASIC for the modern era.
https://www.qb64.org
Other
672 stars 96 forks source link

Regression? Function (with parameters) return variable cannot be read #215

Closed Kroc closed 2 years ago

Kroc commented 2 years ago

At some point this normal code pattern stopped working:

function return_some% (input%)
    if input% > 1 then let return_some% = return_some% + 1
    if input% > 10 then let return_some% = return_some% + 10
    if input% > 100 then let return_some% = return_some% + 100
end function

Basically, you can no longer read back the function's return variable because it is being confused for a function call and the parameter is considered missing. It is very useful to modify the return variable multiple times throughout a function because it avoids having to create a temporary 'result' variable which won't work with exit function used to 'return' the current value.

GeorgeMcGinn commented 2 years ago

You are going to have to give us some better code.

First, it complains that input% name already used, and when I do set it up I get an incorrect number of arguments in the IF statements for return_some%.

I don't see this code working at all. Please post the actual code you are using instead of a hypothetical function.

GeorgeMcGinn commented 2 years ago

Is this what you are trying to do?

` x% = return_some%(500) PRINT x%

FUNCTION return_some% (i%) p% = i% IF i% > 1 THEN p% = p% + 1 IF i% > 10 THEN p% = p% + 10 IF i% > 100 THEN p% = p% + 100 return_some% = p% END FUNCTION `

EDIT: For some reason the code tags aren't working for me.

Kroc commented 2 years ago

Sorry, here's a piece of code that would compile in the past, but no longer does:

FUNCTION Tags_Find% (tag$)
    FOR Tags_Find% = 1 TO UBOUND(Tags)
        IF Tags(Tags_Find%) = tag$ THEN EXIT FUNCTION
    NEXT
    LET Tags_Find% = 0
END FUNCTION

This fails now because FOR Tags_Find% is interpreted as a function call (to itself), and not as the return value

GeorgeMcGinn commented 2 years ago

I'm still getting missing argument errors in the function. If tag$ is not a SHARED variable, then except for your last LET tags_find%=0 statement, each time you have Tags_Find% in your function, it becomes a recursive call, and requires you to pass a parameter to it.

GeorgeMcGinn commented 2 years ago

You might be better off to post this on our forum, where you can post your entire source code there, as I still do not know what this function is supposed to be doing without the rest of the program to see.

Kroc commented 2 years ago

That's the bug -- FOR Tags_Find% = 1 should set the return variable, not attempt a recursive call. That worked in the past, and that's how it works in QB's descendents like Visual Basic

GeorgeMcGinn commented 2 years ago

It thinks it is a recursive call. That is the error I'm getting.

FellippeHeitor commented 2 years ago

@Kroc what you get is the reason why we bumped version number to 2.0. You cannot use a function's name as a variable anymore. It's now always treated as a function call, to properly resemble QuickBASIC 4.5 - our end goal.

FellippeHeitor commented 2 years ago

Relevant forum post from when it was fixed: https://www.qb64.org/forum/index.php?topic=4209.msg135786#msg135786