Question:

What output will be produced by the code below?

func foo(_ number: Int) -> Int {
    func bar(_ number: Int) -> Int {
        return number * 5
    }

    return number * bar(3)
}

print(foo(2))

Correct answer: 30.

Explanation: This code uses inner functions, which means that the foo() function is visible at the end whereas the bar() function is not. The final line calls foo() with a value of 2; this then multiplies that by the result of calling bar()with a value of 3, which in turn multiplies that 3 by 5. So, you get 3 2, i.e. 30.

 


Keywords:

© 2017 QuizBucket.org