Functions can be used like any other type in Swift. This means you can store them in variables, pass them as input to other functions, or return them from functions. It helps you write flexible and reusable code.
When you want to pass a small piece of work (a function) to another function to run later.
When you want to store different actions in variables and choose which one to run.
When you want a function to create and return another function.
When you want to simplify your code by using functions as building blocks.
This function returns another function. We store it in increment and call it with 7.
Swift
func makeIncrementer() -> (Int) -> Int {
func addOne(number: Int) -> Int {
return number + 1
}
return addOne
}
let increment = makeIncrementer()
print(increment(7))
Sample Program
This program stores the multiply function in a variable and uses it to multiply 4 and 5.
Swift
func multiply(a: Int, b: Int) -> Int {
return a * b
}
let operation: (Int, Int) -> Int = multiply
let result = operation(4, 5)
print("Result: \(result)")
OutputSuccess
Important Notes
You can use functions as types to make your code more flexible and easier to change.
Functions as types are useful for callbacks, event handlers, and custom behaviors.
Summary
Functions can be treated like any other type in Swift.
You can store, pass, and return functions using their type signatures.
This helps write reusable and flexible code.
Practice
(1/5)
1. What does it mean when we say "functions are types" in Swift?
easy
A. Functions can be stored in variables and passed around like values.
B. Functions cannot be assigned to variables.
C. Functions must always return an Int type.
D. Functions cannot accept parameters.
Solution
Step 1: Understand the concept of types in Swift
In Swift, types define what kind of values variables can hold.
Step 2: Apply this to functions
Functions have types too, meaning they can be assigned to variables, passed as arguments, or returned from other functions.
Final Answer:
Functions can be stored in variables and passed around like values. -> Option A
Quick Check:
Functions as values = Functions can be stored in variables and passed around like values. [OK]
Hint: Remember: functions behave like any other value in Swift [OK]
Common Mistakes:
Thinking functions can't be assigned to variables
Assuming functions must return Int
Believing functions can't take parameters
2. Which of the following is the correct way to declare a variable that holds a function taking two Ints and returning an Int?
easy
A. var operation: (Int, Int) Int
B. var operation: Int -> Int -> Int
C. var operation: (Int Int) -> Int
D. var operation: (Int, Int) -> Int
Solution
Step 1: Recall Swift function type syntax
Function types are written as (ParameterTypes) -> ReturnType, with parameters separated by commas.
Step 2: Check each option
var operation: (Int, Int) -> Int correctly uses (Int, Int) -> Int. Others have syntax errors or missing arrows.
Final Answer:
var operation: (Int, Int) -> Int -> Option D
Quick Check:
Correct function type syntax = var operation: (Int, Int) -> Int [OK]
Hint: Use (Type1, Type2) -> ReturnType for function types [OK]
Common Mistakes:
Omitting commas between parameter types
Missing arrow '->' in function type
Incorrect parameter grouping
3. What will be the output of the following Swift code?
func add(_ a: Int, _ b: Int) -> Int { a + b }
var mathOperation: (Int, Int) -> Int = add
print(mathOperation(3, 4))
medium
A. 7
B. 34
C. Error: Cannot assign function
D. 0
Solution
Step 1: Understand function assignment
The function 'add' matches the type (Int, Int) -> Int, so it can be assigned to 'mathOperation'.