Skip to content
Justice Tech School

Functions as Values

Functions are values. They can be parameters and return values.

Example
func twice(_ f: (Int) -> Int, _ x: Int) -> Int { f(f(x)) }
print(twice({ $0 + 3 }, 1))
func makeAdder(_ n: Int) -> (Int) -> Int { { $0 + n } }
let add5 = makeAdder(5)
print(add5(10))

Exercise

Function type.

(Int)  Int

Quiz Test

What does makeAdder return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.