QB64-Phoenix-Edition / QB64pe

The QB64 Phoenix Edition Repository
https://qb64phoenix.com
Other
131 stars 26 forks source link

Allow function return values using `EXIT FUNCTION [returnvalue]` #212

Open a740g opened 2 years ago

a740g commented 2 years ago

This was originally requested in https://github.com/QB64Team/qb64/issues/225 using the keyword RETURN.

However, the RETURN keyword is used while returning from GOSUBS and can take an optional label which can lead to complications if it doubles as a return statement for FUNCTIONS.

Hence, the suggestion is to use the following syntax:

EXIT FUNCTION [returnvalue]

Leaving the below link here for future discussions. https://qb64phoenix.com/forum/showthread.php?tid=1100

RhoSigma-QB64 commented 2 years ago

What's wrong with this? All this just to save the additional error assignment? Would seem rather confusing to me to place anything after EXIT FUNCTION. However, it's not a strict NO, but rather a think twice :), I'd certainly not use it, even if it's available.

  FUNCTION foo
      IF wrong THEN foo = failed: EXIT FUNCTION
      foo = success
  END FUNCTION
a740g commented 2 years ago

What's wrong with this? All this just to save the additional error assignment? Would seem rather confusing to me to place anything after EXIT FUNCTION. However, it's not a strict NO, but rather a think twice :), I'd certainly not use it, even if it's available.

Nothing really. I've always used that with QB45. With VB, I switched to using Return within functions. I agree this is just syntactic sugar. However, IMO, it does makes code easier to write and read. Not a high priority stuff. But just wanted to put this here so that we don't forget about it.

ghost commented 2 years ago

I'd definitely use it. If anything, just to force people to upgrade their version of QB64pe so they can run my code 😁

mkilgore commented 2 years ago

I don't know about the proposed syntax (that's easy to change, however), but I think it's worth it to add this since it allows you to avoid writing the function name everywhere you return a value. With the way it currently works if you rename a function you have also change every spot a value is returned, and if you miss one you don't necessarily even get an error. Having a Return-like statement allows you to rename the function without having to change the body of the function at all, which is a big advantage. Additionally it's easier to read, since it's unambiguous between returning a value vs. assigning a variable.

Certainly it's not strictly necessary, but then many of the existing statements are not actually "necessary", they exist purely because they can be used to make your code better. IMO this falls into that same category.

I will say I like the EXIT FUNCTION [returnvalue] syntax less and less the more I see it :P Perhaps just a completely new keyword would be better...

RhoSigma-QB64 commented 2 years ago

With the way it currently works if you rename a function you have also change every spot a value is returned, and if you miss one you don't necessarily even get an error. Having a Return-like statement allows you to rename the function without having to change the body of the function at all, which is a big advantage.

However, if it gets implemented some day, then for consistency END FUNCTION must also be able to take a return value. It makes no sense to allow that for EXIT FUNCTION, but require the assignment to the function name for the regular function end.

a740g commented 2 years ago

Having a Return-like statement allows you to rename the function without having to change the body of the function at all, which is a big advantage.

This is an excellent point.

I will say I like the EXIT FUNCTION [returnvalue] syntax less and less the more I see it :P Perhaps just a completely new keyword would be better...

Now that you said that, I too see the ugliness. 😄 ASM-like _RET perhaps? I dunno. 😁

glasyalabolas commented 2 years ago

Why not just return( value )?

mkilgore commented 2 years ago

Why not just return( value )?

I think the risk of mistakes is a bit high, leaving off the parenthesis would produce a valid (but different) statement. You would likely get an error (unless you were really unlucky) but it would be confusing.

RhoSigma-QB64 commented 2 years ago

To bring in another idea, _LEAVE retval would be something without collisions to other uses in QB64.

a740g commented 2 years ago

To bring in another idea, _LEAVE retval would be something without collisions to other uses in QB64.

I like this.

SteveMcNeill commented 2 years ago

_RETVAL works for me, since RETURN is taken. It's verbose, in the spirit of BASIC, and not a ton of typing.

RhoSigma-QB64 commented 2 years ago

_RETVAL works for me, since RETURN is taken. It's verbose, in the spirit of BASIC, and not a ton of typing.

Sure, but _RETVAL would not necessarily indicate that the function will exit, just that you're willing to set a certain return value. As we look for an EXIT FUNCTION alias, it should be something which indicates that behavior.

ghost commented 2 years ago

In my opinion, EXIT FUNCTION [return value] is the best because we have EXIT CASE, EXIT DO, EXIT WHILE, EXIT FOR, and EXIT FUNCTION