Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / [SOLVED] Function return value declarations that cause compiler errors (and an effective workaround) in AGK Studio tier 1

Author
Message
GemGames
5
Years of Service
User Offline
Joined: 31st May 2018
Location: Edmonton, Alberta, Canada
Posted: 22nd Jan 2024 02:28
A compiler error occurs in AppGameKit Studio tier 1 if a function is defined using both a parameter list and a declared return type.



The compiler will report: 'Error: Unexpected token "as", function declaration must end with a new line'

However, if the same function is defined with a parameter list but without a declared return type, it will compile.



Similarly, if the same function is defined without any parameter list, but with a declared return type, then it will compile.



Notice that the function syntax which triggers a compiler error has both a parameter list and a declared return type. To my knowledge, this is (should be) acceptable syntax. I am not sure if this is a bug in the compiler, or if this detection is intentional.

In any case, an effective workaround is to define the function without a declaration of a return type, even if the function does return a value. This is an imperfect solution, but it seems to work effectively.

Does anyone else have any suggestions or recommendations for a better solution than the workaround I listed? Is it incorrect syntax to have a function which has both a parameter list and a declared return type? Thanks in advance for any suggestions or insights.

The author of this post has marked a post as an answer.

Go to answer

blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Jan 2024 05:20
This post has been marked by the post author as the answer.
The function statement does not have an "as" clause
GemGames
5
Years of Service
User Offline
Joined: 31st May 2018
Location: Edmonton, Alberta, Canada
Posted: 23rd Jan 2024 02:24
Thanks, I suppose I am mixing up dialects of BASIC with AppGameKit tier 1. In VBA for example, it is entirely legal to make the following statement as the 1st line in a function definition:



This line defines the function name, its parameters, and its return type (if any). The above code snippet will generate a compiler error in AGK.

It seems strange to me however, that the following code snippet without parameters is legal syntax in AGK...



...while this following code snippet is illegal syntax in AppGameKit, with the addition of parameters:



This seems inconsistent to me within AGK. But, as long as the "as integer" phrase is omitted, it works well enough in AGK. I suppose this is primarily a matter of semantics amongst some different dialects of BASIC. Programming in one dialect tends to establish programming habits which carry over to other dialects which require different syntax.

Thank you for your valuable input in the previous post, I appreciate that!
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 24th Jan 2024 03:48

Not sure it's supported, it's most likely just ignored.

For example what does it think of a function that's exporting a float.






If it parses the 'as integer' it should notice that mismatch and spit and warning / error.. But I suspect it'll following their existing pattern and use the EndFunction expression to get the return type making the 'as integer' a oversight in the initial prototype.


PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
GemGames
5
Years of Service
User Offline
Joined: 31st May 2018
Location: Edmonton, Alberta, Canada
Posted: 24th Jan 2024 05:45 Edited at: 24th Jan 2024 06:25
Quote: "If it parses the 'as integer' it should notice that mismatch and spit and warning / error.. But I suspect it'll following their existing pattern and use the EndFunction expression to get the return type making the 'as integer' a oversight in the initial prototype."


That's a good point. No mismatch is detected, and no warning or error is generated by the compiler, which leads me to believe that the "as integer" or "as float" is simply ignored by the compiler.

The compiler will issue an error however if a function's internally placed exitfunction's value is given which is of a different variable type than the same function's endfunction's value's type.
For example, the following function will generate a compiler error:



In contrast this function, with an identical body as the previous function, will not generate a compiler error:
GemGames
5
Years of Service
User Offline
Joined: 31st May 2018
Location: Edmonton, Alberta, Canada
Posted: 24th Jan 2024 06:03 Edited at: 24th Jan 2024 06:05
Furthermore, if no return type is given by the endfunction keyword, then if an internal exitfunction command tries to return any value of any type, a compiler error results.

Login to post a reply

Server time is: 2024-05-03 01:26:52
Your offset time is: 2024-05-03 01:26:52