Rouge Documentation Help

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.

apply_discount = function(total: Number) -> Number { return total * 0.9 - 5 }

We applied both coupons in a single numeric operation. However, we can just as much apply both coupons separately in their own statements.

apply_discount = function(total: Number) -> Number { first_discount = total * 0.9 second_discount = first_discount - 5 return second_discount }

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.

area = function(length: Number, width: Number) -> Number { return length * width } area(10, 20)

Output:

200

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.

is_adult = function(age: Number) -> Boolean { return age >= 18 } is_adult(25) is_adult(14)

Output:

true false

To give you another example, the function are_equal checks whether three strings contain the same text.

are_equal = function(a: String, b: String, c: String) -> Boolean { return a == b && b == c } are_equal("hello", "hello", "bye") are_equal("hello", "hello", "hello")
false true

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.

Last modified: 07 January 2026