Skip to content

Function

Justin Mann edited this page Nov 11, 2017 · 3 revisions

Functions have some special behavior

  • last statement in function block is the return value
  • return type is almost always automatically determined
  • arguments can have default values
  • arguments can be passed by name or determined by position

Create a function and call it

func() {
	1
}
func(1)

Call a function using argument position

func(x : 1, y : 2) { x + y }
func(2) // returns 4

Call a function using argument name

func(x : 1, y : 2) { x + y }
func(y : 2) // returns 3

When do I need to specify the return type

Basically whenever you create a loop like this

func() { func() }

Or

foo() { bar() }
bar() { foo() }

You will need to manually specify the type

foo()'i32 { bar() }
bar()'i32 { foo() }

Clone this wiki locally