Functions

You can pack up some chunk of code that you will use a lot into a function, and use this chunk of code by calling your function by name and arguments if provided.

Creating a function.

Creating a function is done by the define keyword followed by a name, then by parentheses and finally a body.


define say_hello () {
    print("Hello!");
    return 0;
}
                    


A function can have 0 or more parameters, which can only be used inside the function's body.


define say_hello_to (person) {
    print("Hello, " + person + "!");
    return 0;
}
                    

Using functions.

You can use functions by 'calling' them.


say_hello();
                    


Calling a function that have parameters must take these parameters' values in the call.


say_hello_to("Mr.P");
                    


Returning from functions.

Returning from functions is getting some useful value back from a function after calling it, this is done using the return keyword followed by the value.

For example, the functions from the previous examples will return zero as specified.


define say_hello_to (person) {
    print("Hello, " + person + "!");
    return 0;
}

//0
let value = say_hello_to("Mr.P");
                    


Anonymous functions.

Anonymous functions are expressions functions that can be created using the keyword lambda followed by a list of arguments, then an arrow (->) and finally an expression or a body. They also can be called.


lambda (person) -> {
    print("Hello, " + person + "!");
    return 0;
}

let one = lambda () -> 1;
one(); //1
                    



Well done, You are close to finish the basics of Pa!