Functions
A function (also known as "coroutine", "subroutine" or "procedure") is a piece of code that is meant to be repeated multiple times throughout a program's lifetime. To prevent duplicating code, a function is used instead.
Without using functions, you would have the following code:
$test = 1
print test is $test
$test = 2
print test is $test
$test = 3
print test is $test
Look at how the
print test is $test
line is repeated multiple times. With a function, we can optimise this code:function showtest $test
print test is $test
end
showtest 1
showtest 2
showtest 3
Now all of the lines in our program are different, which is much more efficient.
You can define functions in two different ways with Wing:
function name $argument
and:
function name $argument do
Both are valid syntaxically speaking, although the developers recommend you use the former. Function definitions support the
do
keyword only for consistency purposes with other blocks.Functions are declared with the
function
keyword, followed by the name of the function. All code below is considered part of the function until the keyword end
is used.function hello
-- This is running inside of the function
print Hello!
end
-- This is running outside of the function
print Hi!
While naming a function with a reserved keyword (e.g.
if
or end
) will not give an error, you will not be able to execute that function."Calling" a function means executing the code it contains. Without specifically calling a function, its code is never executed or even read by the interpreter.
Let's take the
hello
function we declared above. To call it, you will have to run:hello
This runs the
hello
function and then continues running any potential code below it. Variables set from the function remain after the function has finished (for more details, read the Variables and personalities pages).Each function can take a single parameter. A parameter is a variable that is set specifically at the time the function is called. To indicate a function needs a parameter, you simply add a variable name after the name of the function, like this:
function goodbye $name
-- Your code goes here
end
goodbye Me
This is equivalent to doing this:
function othergoodbye
-- Your code goes here
end
$name = Me
othergoodbye
Parameters passed to functions remain even after the function has completed. If your function takes a
$variable
parameter, the $variable
variable will remain after running the function.Following the functions declared above, these lines of code will give errors:
hello Me -- Will give an error because 'hello' doesn't take a parameter
goodbye -- Will give an error because 'goodbye' requires a paremeter
Functions that require more than one parameter can have them set before calling the function, like this:
function complicatedcode
-- Your code here
end
$complicatedcode_value1 = Potato
$complicatedcode_value2 = Carrot
$complicatedcode_value3 = Tomato
complicatedcode
You might be wondering how to use classes with Wing. The answer is simple: you can't. Wing is not an object-oriented language, therefore does not support object-oriented paradigms. This means you cannot use classes and methods in Wing.
Take this JavaScript class:
class Apple {
constructor() {
this.sugar = 10;
}
eat() {
console.log("Ate an apple");
}
}
It can easily be reimplemented like this in Wing:
function apple_create
$apple_created = 1
$apple_sugar = 10
end
function apple_eat
if defined $apple_created do -- This is to check if 'apple_create' has been called before
print Ate an apple
end
end
Wing comes with a number of functions that can get basic tasks done. Obviously, for more advanced tasks, you will need to import additional functions.
Last modified 3mo ago