Functions & Closures
Defining functions, optional type parameters, first-class function values, closures, and recursion.
Defining Functions
Functions are declared with fn. If a function reaches the end without return, it returns missing.
funcs.dl
fn add(a: int, b: int) -> int
return a + b
end
show add(3, 4)First-Class Functions & Closures
Functions are values: you can store them in variables, pass them as arguments, and capture enclosing variables in closures.
closures.dl
fn make_adder(base)
fn inner(x)
return base + x
end
return inner
end
add10 = make_adder(10)
show add10(5) # 15Recursion
Functions can call themselves directly for recursive algorithms:
fib.dl
fn fib(n)
if n <= 1
return n
end
return fib(n - 1) + fib(n - 2)
end
show fib(10)