Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Functions

Total questions: 5

Worksheet time: 3mins

Name
Class
Date
1.

Which keyword is used to define a function in Swift?

a)

func

b)

function

c)

def

d)

method

2.

func sum(_ numbers: Int...) -> Int {

var total = 0

for n in numbers {

total += n

}

return total

}

print(sum(1, 2, 3, 4))

a)

10

b)

1234

c)

Error

d)

nil

3.

func outer() {

func inner() {

print("Inner function")

}

inner()

}

outer()

a)

Inner function

b)

outer

c)

Error

d)

nil

4.

func divide(_ a: Double, _ b: Double) -> Double? {

if b == 0 {

return nil

}

return a / b

}

print(divide(10, 2)!)

a)

5.0

b)

nil

c)

Error

d)

Infinity

5.

func factorial(_ n: Int) -> Int {

if n <= 1 { return 1 }

return n * factorial(n - 1)

}

print(factorial(4))

a)

24

b)

16

c)

4

d)

Error