Sampi

Home Older Versions Documentation Changelog Learn


Functions

What if you are writing a program, and you keep typing say("hello"). That can get annoying. Luckily, this is why there are Functions. Functions are reusable code. You start a function with func, and call it with parentheses.


func greet:
    say("Hello")
end

greet()
        

But what if you want to greet different people? That is what arguments are for. They allow you to give functions information.


func greet name:
    say("Hello, " + name + "!")
end

greet("Alice")
greet("Bob")
        

Now you can greet new people! But what if you what to get a value back from the function? That is what the return keyword is for.


func square x:
    return x * x
end

say(square(2))
        

Previous Next