QB64Team / qb64

BASIC for the modern era.

Home Page:https://www.qb64.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

a740g opened this issue · 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

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

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

commented

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

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

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.