Skip to content

Functions

Vihan edited this page Mar 2, 2018 · 1 revision

Functions are one of the most fundamental parts of a program. Functions take in some values, and do something. Some functions can return a value too. In VSL functions are first-class objects meaning you can pass them around like any other variable. Lets take a look at the anatomy of a basic function in VSL:

func add(x: Int, to y: Int) -> Int {
    return x + y
}

This function takes in two arguments x (of type Int) and y (also of type Int). Will run some code, and return their sum (in type Int).

At the low-level, VSL is compatible with CCC (the C Calling Convention). This means that you can seamlessly use your C-functions with VSL functions using the external(...) syntax described in the C interoperability section.

Return Types

Functions can either return a value, or not. Functions that don't return values are called void functions. To declare the return type of a function you use the -> T syntax. An example of a function returning an integer:

func returnFirstNumber() -> Int { return 1 }

Void Functions

Declaring a void function in VSL is nothing special however if you would like to explicitly indicate no value is returned:

func sayHi(to person: Person) -> Void {
    print("Hello #{person.name}!")
}