0
0
Swiftprogramming~10 mins

Nested functions in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a nested function inside the outer function.

Swift
func outer() {
    func [1]() {
        print("Hello from inner")
    }
    inner()
}
Drag options to blanks, or click blank then click option'
Aprint
Bouter
Cinner
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Using the outer function name inside the nested function definition.
Trying to call a function that is not defined.
2fill in blank
medium

Complete the code to return the result of the nested function.

Swift
func outer() -> Int {
    func [1]() -> Int {
        return 5
    }
    return inner()
}
Drag options to blanks, or click blank then click option'
Ainner
Bouter
Cresult
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the outer function name instead of the nested function.
Using a name not defined as a function.
3fill in blank
hard

Fix the error in the nested function call by completing the blank.

Swift
func outer() {
    func inner() {
        print("Inside inner")
    }
    [1]()
}
Drag options to blanks, or click blank then click option'
Aouter
Binner
Cprint
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the outer function inside itself.
Using a function name that does not exist.
4fill in blank
hard

Fill both blanks to create a nested function that adds two numbers and returns the sum.

Swift
func addNumbers(a: Int, b: Int) -> Int {
    func [1]() -> Int {
        return a [2] b
    }
    return sum()
}
Drag options to blanks, or click blank then click option'
Asum
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator like - or *.
Naming the nested function differently than the call.
5fill in blank
hard

Fill all three blanks to create a nested function that multiplies a number by itself and returns the result.

Swift
func calculateSquare(number: Int) -> Int {
    func [1]() -> Int {
        return number [2] number
    }
    return [3]()
}
Drag options to blanks, or click blank then click option'
Asquare
B*
C+
Dmultiply
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Calling a function name that does not exist.