QB64Team / qb64

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

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

Open a740g opened 2 years ago

a740g commented 2 years ago

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

GeorgeMcGinn commented 2 years ago

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).

jakebullet70 commented 2 years ago

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

aouwt commented 2 years ago

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

jakebullet70 commented 2 years ago

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

mechatronic3000 commented 2 years ago

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.