A function is a special block of code that doesn't do anything immediately. It gets saved as a variable so that it can be run at another time, zero or more times.
From the description above, we can see that there are two stages in the lifetime of a function.
- Creating the function
- Running the function
There are two ways to create a function, but they are very similar. The first way is like this:
1 2 3 4
And the second way to write the same function is like this:
1 2 3 4
The difference is that if you follow the first approach, Modulo1 will make the function available before the app runs. But if you take the second approach, Modulo1 will make the function available while the app is running, based on where the function appears in the code. For example, if the function is written at the very bottom of your code, then the function might be loaded last. And this could cause an error if an earlier line of code tries to run that function.
There is, however, a advantage to using the second approach. Functions that are created in this way, are able to be overwritten, and/or deleted. For example:
1 2 3 4 5 6 7 8 9 10
In this example, we have created a function called
doSomething, but if the result it produces is greater than
1000, it will be overwritten by a slightly different function.
Now, we ought to go into some details about this syntax:
1 2 3 4
We begin with the
function keyword followed by the name of our function, which is
doSomething. Then, we provide a list of inputs that our function requires.
Each input in the list has two words, and the pairs of words are separated by commas. The first word within each pair is the data-type that our class is expecting for that input. And the second word is the identifier that our class intends to give to that input.
And therefore, our
doSomething function is expecting a
number as the first input and intends to identify it as
x. And the function is expecting a
number as the second input and intends to identify that as
Next, we have to let Modulo1 know what kind of variable our function will return as an output. We use the
returning keyword, followed by the name of a data-type or class definition. In this case, we're going with the
A function does not have to return anything. If your function will not return any output, then you can type
returning null to specify that this is the case.
With all that out of the way, we can write our function within the curly braces:
In this case, we are only performing a very trivial and pointless calculation, but in real situation a function can include all of the same kinds of instructions that exist outside of functions (except for starting main blocks or declaring class definitions).
Finally, we return the result of that calculation by using the
return keyword, followed by the variable or expression that we want to output.
Running our function is simple. But the syntax differs according to what we intend to do with the function's output (and whether our function returns any output at all).
This is how run the function, and store the function's output in a new variable:
var keyword tells Modulo1 that we are creating a new variable, and
result is the name we have chosen for that new variable. Next we have an equals (
=) symbol followed by the name of our function, and the values that we are going to provide as input. In this case, we are providing the numbers
34 as inputs.
This is how we run the same function, but instead using the function's output to overwrite an existing variable:
And if we're running a function that doesn't have any output (or if we just want to discard the output):
A property is a special feature of a variable that can be used to get and/or set some aspect of that variable. When using the properties of functions, it is important to exclude the parentheses
() that are usually present after the function's name. For example, if we want to use the
type property on a function called
calculate(), we would write:
The returns property can be used to find out the data-type that a particular function returns
The type property can be used to find out the data-type of a particular function. The result will always be