Skip to content

Code Structures

Controlling the flow of the app

Sometimes we need Modulo1 to do something other than just read our code from top to bottom. We might need it to make a decision before deciding what to do next, or we might need it to perform a set of instructions repeatedly. The way to achieve this, is through flow control statements.

Modulo1 includes the same kinds of flow control statements that exist in most other programming languages:

  • while loop statements
  • do/while loop statements
  • for loop statements
  • switch statements
  • if statements

Info

For more information, check out our page on flow control


Creating functions

A function is a group of instructions that are saved in memory, which can be run at a later time. Functions can be stored in variables, or they can be stored in the core of Modulo1. The difference is that if you store a function in a variable, you are permitted to delete it or overwrite it at any time. But if you store a variable in the core, it can never be changed or deleted.

Here is an example of creating a function and storing it as a variable:

1
2
3
var getTen = function() returning number { 
    return 10; 
};

It can be deleted like this:

1
delete getTen();

Here is an example of creating the same function, but storing it in the core:

1
2
3
function getTen() returning number {
    return 10;
};

Attempting to delete or modify this, will cause an error.

Sometimes, we might want to be able to provide one or more variables as input that can be processed by the function's code. In this situation, we might create the following:

1
2
3
function multiplyByTen(number x) returning number {
    return x * 10;
};

In this example, we have specified that our function will accept one number as input. Additionally, we have specified that inside the function, this inputted value will be stored in the variable x. Finally, we can use x inside our function's code.

Info

For more information, check out our page on functions


Running functions

The purpose of creating a function, is so that it can be run one or more times. Some functions result in a value being created as a result. This is called returning a value.

Here is an example of running a function that returns a number.

1
var result = multiplyByTen(12);

The variable result will now hold the value that was returned by the function, which is 120.

Some functions, however, are not intended to return a value. In these cases, we can just say that it returns null. Then, Modulo1 will allow us to either return the value null or return nothing at all. For example:

1
2
3
function writeOutputMessage() returning null {
    print("This message will be shown after the app runs");
};

This function can be run with the following instruction:

1
writeOutputMessage();

Info

For more information, check out our page on functions


Creating structures

Sometimes it is useful to organise a group of functions and variables together into a reusable structure. This type of structure appears in many other programming languages, and is known as a class.

A class cannot be used directly. Instead, it is a definition of a structure that you can create at a later point in your code. Or in other words, a class is basically a custom data type that you can design.

Here is an example of how to define a class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Human(number age, string firstName, string lastName) {
    public {
        function getAge() returning number {
            return age;
        }

        function getGreeting() returning string {
            var fullName = firstName + " " + lastName;
            return "Hello " + fullName + "!";
        }
    }
}

To put this class definition into use, we must ask Modulo1 to create a new variable that will hold an instance of this class:

1
var hobbit = Human(111, "Bilbo", "Baggins");

This line of code, created a variable of the Human data-type. Its functions can be run like this:

1
2
var hobbitAge = hobbit.getAge();
var message = hobbit.getGreeting();

Info

For more information, check out our page on classes


Declaring errors

Modulo1 will automatically attempt to stop the app and notify you of an error if one of its rules have been violated. However, there might be some cases where you want to manually cause the app the stop, due to an unexpected and undesirable occurrence. In those situations, you can declare a custom error:

1
error >>>{ message : "Something bad happened" };

You can also choose to attach other data to the error. For example:

1
2
3
4
error >>>{ 
    message : "Something bad happened",
    someValue : 37
};

In this example, we have attached a variable to this error, called someValue, but we could have called it anything we like, so long as we follow the rules of variable naming. By the way, you can add as many variables as you like to an error.

Info

For more information, check out our page on errors


Recovering from errors

Errors are useful at communicating that something has gone wrong. But sometimes it is ideal for an app to be able to continue running and recover from an error, especially if there is a strong likelihood of human error. You can achieve this by enclosing any part of your code in a pair of "Try/Catch" blocks:

1
2
3
4
5
6
7
try {
    var result = riskyCode();
}

catch (err) {
    print("We caught an error: " + err.message);
}

You can put whatever code you like within the try block. And if any error is detected, when the try block runs, Modulo1 will immediately stop processing the block. Then it will run the catch block.

Info

For more information, check out our page on errors