📖 Scenario: Imagine you are creating a simple calculator app that can add and multiply numbers. To keep your code organized, you want to use nested functions inside a main function.
🎯 Goal: Build a Swift program that uses nested functions to add and multiply two numbers, then prints the results.
📋 What You'll Learn
Create a main function called calculate that takes two integers as input.
Inside calculate, create a nested function called add that returns the sum of the two numbers.
Inside calculate, create another nested function called multiply that returns the product of the two numbers.
Call both nested functions inside calculate and print their results.
💡 Why This Matters
🌍 Real World
Nested functions help organize code in apps like calculators or games where small helper functions are needed inside bigger functions.
💼 Career
Understanding nested functions is useful for writing clean, readable code in Swift, which is important for iOS app development jobs.
Progress0 / 4 steps
1
Create the main function
Write a function called calculate that takes two parameters: num1 and num2, both of type Int.
Swift
Hint
Use the func keyword to define the function with two Int parameters.
2
Add nested functions for addition and multiplication
Inside the calculate function, write two nested functions: add and multiply. Both take no parameters and return an Int. add returns the sum of num1 and num2. multiply returns the product of num1 and num2.
Swift
Hint
Define add and multiply inside calculate using func. Use return to send back the results.
3
Call nested functions and print results
Still inside calculate, call the nested functions add() and multiply(). Store their results in variables sumResult and productResult.
Swift
Hint
Call the nested functions by their names followed by parentheses, then assign their results to variables.
4
Print the results
Inside calculate, print the values of sumResult and productResult using print. Then, call calculate with num1 = 4 and num2 = 5 outside the function.
Swift
Hint
Use print with string interpolation to show the results. Then call calculate(num1: 4, num2: 5) outside the function.
Practice
(1/5)
1. What is a nested function in Swift?
easy
A. A function that calls itself
B. A function that returns another function
C. A function defined inside another function
D. A function defined outside any class or struct
Solution
Step 1: Understand the definition of nested functions
Nested functions are functions written inside other functions to organize code better.
Step 2: Match the definition to the options
A function defined inside another function correctly describes a function inside another function, which is the definition of nested functions.
Final Answer:
A function defined inside another function -> Option C
Quick Check:
Nested function = function inside function [OK]
Hint: Nested means inside another function [OK]
Common Mistakes:
Confusing nested with recursive functions
Thinking nested functions are global
Mixing nested with returning functions
2. Which of the following is the correct syntax to define a nested function in Swift?
easy
A. func outer() { func inner() { } }
B. func outer() { func inner; }
C. func outer() { inner func() { } }
D. func outer() { func inner() }
Solution
Step 1: Review Swift function syntax
Functions are defined with 'func name() { }'. Nested functions follow the same syntax inside another function.
Step 2: Check each option for correct syntax
func outer() { func inner() { } } uses correct syntax with braces for both outer and inner functions. Others have missing braces or wrong order.
Final Answer:
func outer() { func inner() { } } -> Option A
Quick Check:
Nested function syntax = func inside func with braces [OK]
Hint: Look for 'func' with braces inside another function [OK]
Common Mistakes:
Omitting braces for inner function
Using semicolon instead of braces
Placing 'func' keyword incorrectly
3. What will be the output of the following Swift code?
D. innerFunc() called before its definition causes error
Solution
Step 1: Analyze function call order
The code calls 'innerFunc()' before it is defined inside 'calculate()'. Swift requires functions to be defined before use.
Step 2: Understand Swift's function declaration rules
Calling a nested function before its declaration causes a compile-time error.
Final Answer:
innerFunc() called before its definition causes error -> Option D
Quick Check:
Call before definition = error [OK]
Hint: Define nested functions before calling them [OK]
Common Mistakes:
Assuming nested functions can be called before definition
Thinking missing return type causes error here
Believing nested functions can't be defined inside
5. How can you use nested functions to create a function that returns another function which adds a fixed number to its input?
Choose the correct Swift code.
hard
A. func makeAdder(_ x: Int) -> Int {
func adder(y: Int) -> Int {
return x + y
}
return adder(5)
}
B. func makeAdder(_ x: Int) -> (Int) -> Int {
func adder(y: Int) -> Int {
return x + y
}
return adder
}
C. func makeAdder(_ x: Int) -> (Int) -> Int {
return func(y: Int) -> Int { x + y }
}
D. func makeAdder(_ x: Int) -> (Int) -> Int {
func adder(y: Int) -> Int {
return y
}
return adder
}
Solution
Step 1: Understand the goal
We want a function that returns another function adding a fixed number 'x' to input 'y'.
Step 2: Check each option's logic
func makeAdder(_ x: Int) -> (Int) -> Int {
func adder(y: Int) -> Int {
return x + y
}
return adder
} defines nested 'adder' using 'x' and returns it correctly. func makeAdder(_ x: Int) -> Int {
func adder(y: Int) -> Int {
return x + y
}
return adder(5)
} returns a value, not a function. func makeAdder(_ x: Int) -> (Int) -> Int {
return func(y: Int) -> Int { x + y }
} has invalid syntax. func makeAdder(_ x: Int) -> (Int) -> Int {
func adder(y: Int) -> Int {
return y
}
return adder
} returns a function ignoring 'x'.
Final Answer:
func makeAdder(_ x: Int) -> (Int) -> Int {
func adder(y: Int) -> Int {
return x + y
}
return adder
} -> Option B
Quick Check:
Nested function returns function adding x = func makeAdder(_ x: Int) -> (Int) -> Int {
func adder(y: Int) -> Int {
return x + y
}
return adder
} [OK]
Hint: Return nested function that uses outer parameter [OK]
Common Mistakes:
Returning a value instead of a function
Incorrect nested function syntax
Ignoring outer function parameter inside nested function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.