Function Return Values
Scrybe allows you to return any type from a function besides lists. To specify a return type, prepend any one of these types to the function
keyword:
num
- Return a numberstr
- Return a stringbool
- Return a booleanvar
- Return a general variable
A function declared without a return type will not work in an expression.
To actually return a value, simply use the return
keyword as you would in most other programming languages. Here’s an example of function return values in action:
warp num function factorial(n) {
result: num = 1;
for (i = n; i > 0; i -= 1) {
result *= i;
}
return result;
}
scratch.on_flag() {
while (true) {
scratch.ask("What would you like to calculate the factorial of?");
answer: str = scratch.answer;
result: num = factorial(answer);
say_for_seconds("The factorial of " .. answer .. " is " .. tostr(result), 2);
}
}
Alternatively, you can use return
without any value to simply stop the script where it is:
warp num function factorial(n) {
if (n <= 0)
return;
...
You may also use return
inside of a hat to do the same thing.
Examples
Here are some other examples of function return types:
warp num function rshift(x, bits) // Returns a number
return x / 2 ** bits;
warp bool function is_on_top() // Returns a boolean
return this.y >= 0;
warp str function get_greeting(name) { // Returns a string
if (time.hour < 12) return "Good morning, " .. name .. "!";
if (time.hour < 5) return "Good afternoon, " .. name .. "!";
return "Good evening, " .. name .. "!";
}