qb64 icon indicating copy to clipboard operation
qb64 copied to clipboard

Allow function names using "as <type>" & "Return" keyword

Open a740g opened this issue 2 years ago • 5 comments

Currently function names require type characters. For example:

Function test1% (x As Integer, y As Integer) test1% = x + y End Function

This is ok. However, also allow the following:

Function test1 (x As Integer, y As Integer) As Integer test1% = x + y End Function

Allowing the use of the "return" keyword would be a bonus. Like Function test1 (x As Integer, y As Integer) As Integer Return x + y End Function

a740g avatar Apr 01 '22 19:04 a740g

I think that using RETURN, like other languages do, would make sense if we didn't need it for GOSUBs. I believe this question was raised awhile back (either on Discord or the Forum), and there was an issue with GOSUBs (and backwards compatibility with QBasic).

GeorgeMcGinn avatar Apr 01 '22 21:04 GeorgeMcGinn

How about a _RETURN instead, The underscore is used for other keywords.

jakebullet70 avatar Apr 02 '22 12:04 jakebullet70

What if somebody uses $NOPREFIX? that'd still cause conflict

aouwt avatar Apr 02 '22 14:04 aouwt

Thinking just provide an error message that you cannot use both at the same time.

jakebullet70 avatar Apr 02 '22 16:04 jakebullet70

Is it even necessary to modify or use RETURN? What if it was like this?

Function test1 (x As Integer, y As Integer) As Integer test1 = x + y End Function

This is pretty much how it is currently done. But it would still allow you to use the AS keyword to set the return type.

Setting the return type of a function to a custom type would be helpful. Currently only the arguments are allowed to be a custom type. For Example: ` TYPE tVECTOR x AS INTEGER y AS INTEGER END TYPE

FUNCTION add (v AS tVECTOR, a AS INTEGER) AS tVECTOR add.x = v.x + a add.y = v.y + a END FUNCTION

FUNCTION subtract (v AS tVECTOR, a AS INTEGER) AS tVECTOR subtract.x = v.x - a subtract.y = v.y - a END FUNCTION

That way I can nest functions in a single line. For Example.

DIM AS INTEGER scalarAdd, scalarSub DIM AS tVECTOR vIN, vOUT

vOUT = subtract(add(vIN,scalarAdd),scalarSub)`

Sorry If I Hijacked this thread.

mechatronic3000 avatar Apr 07 '22 14:04 mechatronic3000