Functions definition

To define function uses the def keyword:

def function(arg1, arg2) {
  println arg1
}

Shorthand definition

There is short syntax fot function body:

def repeat(str, count) = str * count

Which is equivalent to:

def repeat(str, count) {
  return str * count
}

Default arguments

Function arguments can have default values.

def repeat(str, count = 5) = str * count

In this case only str argument is required.

repeat("*")     //  *****
repeat("+", 3)  //  +++

Default arguments can't be declared before required arguments.

def repeat(str = "*", count) = str * count

Causes parsing error: ParseError on line 1: Required argument cannot be after optional

Inner functions

You can define function in other function.

def fibonacci(count) {
  def fib(n) {
    if n < 2 return n
    return fib(n-2) + fib(n-1)
  }
  return fib(count)
}

println fibonacci(10) // 55