Gosu functions
Functions encapsulate a series of Gosu statements to perform an action and optionally return a value. Generally speaking, functions exist attached to a type. For example, declaring functions within a class. As in other object-oriented languages, functions declared on a type are also called methods.
In the context of a Gosu program (a .gsp file), you can declare functions
at the top level, without attaching them explicitly to a class. You can
then call this function from other places in that Gosu program.
print function is special because
it is always in scope, and is not attached to a type. It is the only
true global function in Gosu.Gosu does not support functions defined within other functions. However, you can use the Gosu feature called blocks to do something similar.
Unlike Java, Gosu does not support variable
argument functions (so-called vararg
functions), meaning that Gosu does not support arguments with “...” arguments.
Gosu permits you to specify only type literals for a function’s return type. Gosu does not support other expressions that might evaluate (indirectly) to a type.
Gosu requires that you
provide the return type in the function definition, unless the return
type is void (no return
value). If the return type void, omit the type and the colon before it.
Also, any return statement
must return a type that matches the declared function return type. A
missing return type or a mismatched return value generates a compiler
error.
Syntax
[modifiers] function IDENTIFIER( argument-declaration-list ) [:type-literal] {
function-body
}Examples
function square( n : int ) : int {
return n * n
}
// Compile error "Cannot return a value from a void function."
private function myfunction() {
return "test for null value"
}
function fibonacci( n : int ) : int {
if (n == 0) { return 0 }
else if (n == 1 ) { return 1 }
else {return fibonacci( n - 1 ) + fibonacci( n - 2 ) }
}
function concat ( str1:String, str2:String ) : String {
return str1 + str2
}
If the return type is not void, all possible code paths
must return a value in a method that declares a return type.
In other words, if any
code path contains a return
statement, Gosu requires a return statement for all possible paths through
the function. The set of all paths includes all outcomes of conditional
execution, such as if
and switch statements.
For example, the following method is invalid:
// Invalid...
class MyClass {
function myfunction(myParameter) : boolean {
if (myParameter == 1) {
return true
}
if (myParameter == 2) {
return false
}
}
}
Gosu generates a “Missing Return Statement” error
for this function and you must fix this error. The Gosu compiler sees
two separate if expressions
for a total of four total code paths. Even if you believe the function
is always used with myParameter
set to value 1 or 2 but no other value, you must fix the error. To fix
the error, rewrite the code so that all code paths contain a return statement.
For example, you can fix the earlier example
using an else clause:
class MyClass {
function myfunction(myParameter) : boolean {
if (myParameter == 1) {
return true
} else {
return false
}
}
}
Similarly, if you use a switch statement, consider using
an else section.
This strict requirement for return statements mirrors the
analogous requirements in the Java language.
See also
