Advanced functions
In the last chapter, we learned how to make out computations reusable by creating and calling functions. However, we stuck to simple numeric calculations. In this chapter, we would like to expand on the topic of functions to show you how powerful they can be.
Multiple statements
In the previous chapter we mentioned that a function can contain multiple statements. However, we have yet to show you an example of such a function. Let's use the same discount example we used previously.
We applied both coupons in a single numeric operation. However, we can just as much apply both coupons separately in their own statements.
As you can see, we can use any statement we would use outside a function inside the function as well. However, we need to remember to still place a return statement at the end of the function.
Multiple inputs
Functions are not limited to a single input. You can use as many inputs to your function as you would like. The following example shows a function that calculates the area of a rectangle, with each of the inputs separated by a comma.
Output:
Using other data types
We have only looked at functions that operate on the data type Number. However, functions can receive inputs as well as return values in any data type. This is done by simply changing the type literals for the inputs as well as the return type. The following function determines whether a person is an adult based on their age.
Output:
To give you another example, the function are_equal checks whether three strings contain the same text.
To recap, we looked at functions that take more than one parameter, functions that work with any data type and functions that can contain multiple statements. This knowledge covers the most important things that functions allow you to do. However, functions do have even more to offer, which we will take a look at in some of the following chapters.