📖 Scenario: You are building a simple calculator app that can perform different math operations. Each operation is a function that takes two numbers and returns a number.
🎯 Goal: Learn how to use functions as types by creating variables that hold functions, then calling them to perform calculations.
📋 What You'll Learn
Create a function variable with a specific function type
Assign different functions to the function variable
Call the function variable with arguments
Print the result of the function call
💡 Why This Matters
🌍 Real World
Using functions as types lets you write flexible code that can change behavior by swapping functions. This is useful in apps like calculators, games, or any place where you want to choose actions dynamically.
💼 Career
Understanding functions as types is important for Swift developers to write clean, reusable, and flexible code. It is a foundation for advanced topics like closures, callbacks, and functional programming.
Progress0 / 4 steps
1
Create two functions for addition and subtraction
Write two functions called add and subtract. Both take two Int parameters and return an Int. add returns the sum, subtract returns the difference.
Swift
Hint
Define two functions with the same parameter and return types but different bodies.
2
Create a function variable with type (Int, Int) -> Int
Create a variable called operation of type (Int, Int) -> Int. Assign the add function to operation.
Swift
Hint
Use the exact variable name operation and assign add without parentheses.
3
Change the function variable to subtract and call it
Assign the subtract function to the operation variable. Then call operation with arguments 10 and 4, and store the result in a variable called result.
Swift
Hint
Assign subtract to operation and call it with 10 and 4.
4
Print the result of the operation
Print the value of the result variable using print.
Swift
Hint
Use print(result) to show the output.
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'.