Easypt

Functions

A function is a group of statements that perform particular task.

Advantages

The advantages of breaking a program into subroutines [functions] include:

Disadvantages

From Wikipedia

1. What we call a function in Easypt

Basically we can call everything of Callable type. NativeCallable is type of function provided by the Easypt interpreter or native libraries. BlockCallable is type of function defined in *.ez source code.

2. Defining function

Let’s begin with function literal (unnamed function):

{
    log("I am BlockCallable literal!");
};

Nothing happened. We need to call it (by call operator - ()).

{
    log("I am BlockCallable literal!");
}();

(Code called like this above - anonymous function literal - has no access to current context.)

Let’s give our function a name:

auto fun.=({
    log("I am fun!");
});
fun();

3. Parameters

In computer programming, a parameter […] is a special kind of variable, used in a subroutine [function] to refer to one of the pieces of data provided as input to the subroutine. […] Argument in computer science is the actual input expression passed/supplied to a function.

From Wikipedia

In Easypt arguments are provided as Array of references to arguments named args. See the example:

auto print.=({
    log(args[0].get(), args[1].get());
});
print(7, 3.14);

Expected output is:

7
3.14

Try to run:

auto print.=({
    log(args[0].get(), args[1].get());
});
print(7);

You will see out of range exception:

Exception at: ..Root.import
Exception at: ..Root.my_source_file
Exception at: ..Root.my_source_file.callOperator
Exception at: ..Root.my_source_file.print
Exception at: ..Root.my_source_file.print.callOperator
Exception at: ..Root.my_source_file.print.args
Exception at: ..Root.my_source_file.print.args.readOperator
OutOfRangeException: Out of range while calling ..Root.my_source_file.print.args.readOperator

But

print(7, 7, 7, 7, 7);

works fine as you expected.

So what are those “references”? Run this:

auto myIncrement.=({
    args[0].get().++();
});
auto myInt.=(5)
myIncrement(myInt);
log(myInt);

Output is 6. If you don’t want to pass a reference, you will have to use Object’s copy method:

auto myIncrement.=({
    args[0].get().++();
});
auto myInt.=(5)
myIncrement(myInt.copy());
log(myInt);

Now output is 5.

You will learn about variable number of arguments while learning about loops.

4. Return

In computer programming, a return statement causes execution to leave the current subroutine [function] and resume at the point in the code immediately after where the subroutine was called […]. Return statements in many languages allow a function to specify a return value to be passed back to the code that called the function.

From Wikipedia

In Easypt return is BlockCallable’s method. Calling return makes execution to leave the current function and return’s argument is the value of function call. Example:

auto fun.=({
    return(5);
});
log(fun());

Expected output:

5

5. Exercise “Birthday Project”

Your exercise is to analyze following code:

auto makeGreetingsString.=({
    auto str.=("Happy ");
    str.=(str.+(args[0].get().toString()));
    str.=(str.+(" birthday "));
    str.=(str.+(args[1].get()));
    str.=(str.+("!"));
    return(str);
});
log(makeGreetingsString(50, "John"));

Output:

Happy 50 birthday John!

Understand the code:


Next lesson (Conditionals and loops)

Table of contents