Repository navigation
Functions
Functions in Lantern are a way to bundle reusable behavior. In fact, the entrypoint of every Lantern program is itself a function! It provides the behavior of the program when executed.
Functions are defined using the fun keyword and can have any number of parameters and a return type. For example, take a look at this function definition:
fun add(a: int, b: int) -> int {
val sum = a + b;
return sum;
}
This function is named add and it takes two different parameters, a and b. In this case, both of these parameters are integers. Additionally, it returns an int back to the caller. In the function body, we are doing a simple addition calculation and storing in the sum variable. Then, we return that sum and give it back to the caller. Calling this function should look familiar:
add(2, 5);
We can also store the result of the function (what it returns) in a variable:
val sum = add(2, 5);
Note that sum here is an int, since the add function returns an int.