Skip to content
Jacob Misirian edited this page Aug 27, 2016 · 4 revisions

Like most every other programming language, Hassium allows you to define your own functions to use and call at your leisure. The parts of a function consist of the name, the parameter list, the body, and the return value.

1.1 Declaring Functions

The syntax for defining a function is func <name> (<parameter> [: type]) [: returnType] { <body> } To define a function in the global namespace. Move your cursor to under the closing } after main and type the following:

func add (x, y) {

}

This is a blank function named add that accepts two arguments of any type named x and y. Let's add a body to this function:

func add (x, y) {
    println ("You passed: " + x);
    println ("You passed: " + y);
}

Call this function in the main method by running:

func main () {
    add (5, 7);
}

You should see the output on your screen You passed: 5\nYou passed: 7. This is because when you sent in the integers 5 and 7, they became the value that was assigned to the parameters x and y.

1.2 Returning from a Function

The last part of a function is the return value. This is the value that a function sends back after executing it's block of code. For add, we would want to send back an integer that is the result of x + y. This can be done using the return keyword, which will take an expression and send it back to the caller. Once again, let's modify add.:

func add (x, y) {
    return x + y;
}

Now when we run our script again we should just see the number 12. This is because we passed in 5 and 7 as x and y, the add function added them together and used the return keyword to send back the result. We can call add as much as we want with different numbers.

You can define a function with the same name but different sets of parameters. This is called defining a multifunc, and the compiler does it for you. Let's revisit our add function.

func add (x, y) {
    return x + y;
}
func add (x, y, z) {
    return x + y + z;
}

As you can see, we've added a second function also named add that takes in three parameters, the third being z, and returns the sum of all three instead of just two. Now in the main method we can call either by using different parameters:

func main () {
    println (add (4, 5));    # Calls the add with two parameters.
    println (add (4, 5, 6)); # Calls the add with three parameters.
}

1.3 Variadic Parameters

Variadic parameters are parameters that allow for an infinite amount of parameters to be added to the function call that map to a list for the function to use. Variadic parameters should be the last parameter in the function declaration. A variadic parameter is declared using the params keyword:

func test (str : string, i : int, params args) {
    println ("Passed " + str);
    println ("Passed " + i);
    foreach (arg in args) {
        println ("Passed " + arg);
    }
}

When this function is called, it demands an integer, followed by a string, followed by an infinite amount of any parameter. There are two ways to call this from main:

func main () {
    test ("one", 2, "three", 4, "five", 6);
    test ("one", 2, ["three", 4, "five", 6]);
}

Both of these calls produce the same output, in the first call, the last three arguments are added to the list args parameter. In the second one, the list is simply passed as the args.

1.4 Type Enforcement

The last nuance to function making is specifying parameter and return types. Our add function currently works because we know that it should take in two integers. But what if a less experienced user passed in a string to add, like so:

func main () {
    println (add ("4", 5));
}

This code prints 45, since "4" is a string, and string + int will result in a string. To enforce proper typing let's modify the add function:

func add (x : int, y : int) {
    return x + y;
}

Now when the user tries to pass in a string, it will give a runtime exception saying that an argument was not of the proper type.

Much like parameters, the type of the return value can also be enforced, while not as important this can still make your code much more readable. Here is a complete example of the add function with type enforcement:

func add (x : int, y : int) : int {
    return x + y;
}

The : int after the closing ) detonates that this function will return an int.

Clone this wiki locally