In this lab, students will practice working with functions, closures, and higher-order functions in Swift. They will learn how to declare and use functions with various parameters and return types, explore function overloading and nested functions, and understand closures and their syntax. Additionally, they will apply higher-order functions like map, filter, and reduce to manipulate data efficiently.
Instructions:
Create a Swift playground or Xcode project to complete the tasks.
Follow the step-by-step instructions for each task below.
Write your code in a clear and organized manner, following best practices.
Your code must be logically correct.
Requirements:
Xcode v13 or higher installed
Swift compiler v5.7 or higher installed
Tasks:
Task 1: Function Basics
Create a function greetUser that takes a String parameter and returns a greeting message.
Call the function with different names and print the output.
Task 2: Function Overloading
Write two functions named calculateArea:
One that takes the radius of a circle and returns its area.
Another that takes the length and width of a rectangle and returns its area.
Call both functions with appropriate arguments and display the results.
Task 3: Nested Functions
Define a function mathOperation that takes two integers and a string representing an operation ('add', 'subtract', 'multiply', 'divide').
Inside mathOperation, create nested functions for each operation and return the result accordingly.
Call mathOperation with different numbers and operations.
Task 4: Higher-Order Functions
Create an array of integers and use the map function to transform each element by adding 1 to it.
Use the filter function to keep only even numbers from the array.
Utilize the reduce function to find the sum of all elements in the array.
Task 5: Closures
Create a closure that takes integer and multiply it by itself and returns the result of this multiplication.
Create an array of integers and use the map function and pass your closure to the map function, finally print each element using forEach function.
Submission Guidelines:
Create new repository name it as "iOS-Developemnt-Bootcamp-July-2023-Week-1-Day-3-Lab".
Upload your project files to the repo you just created.
Submit your project repo link as comment down below.
Grading Criteria:
Your lab assignment will be graded based on the following criteria:
Correctness and completion of tasks (50%)
Code quality, readability, and adherence to best practices (50%)
Additional Notes:
You may ask the lab instructor for assistance during lab hours.
Avoid plagiarism; write the code yourself.
Late submissions will not be accepted without prior approval.
Deadline:
Submit your completed lab assignment prior start of next session.
Objective:
In this lab, students will practice working with functions, closures, and higher-order functions in Swift. They will learn how to declare and use functions with various parameters and return types, explore function overloading and nested functions, and understand closures and their syntax. Additionally, they will apply higher-order functions like map, filter, and reduce to manipulate data efficiently.
Instructions:
Requirements:
Tasks:
Task 1: Function Basics
greetUser
that takes aString
parameter and returns a greeting message.Task 2: Function Overloading
calculateArea
:Task 3: Nested Functions
mathOperation
that takes two integers and a string representing an operation ('add', 'subtract', 'multiply', 'divide').mathOperation
, create nested functions for each operation and return the result accordingly.mathOperation
with different numbers and operations.Task 4: Higher-Order Functions
map
function to transform each element by adding 1 to it.filter
function to keep only even numbers from the array.reduce
function to find the sum of all elements in the array.Task 5: Closures
map
function and pass your closure to themap
function, finally print each element usingforEach
function.Submission Guidelines:
Grading Criteria:
Your lab assignment will be graded based on the following criteria:
Additional Notes:
Deadline:
Submit your completed lab assignment prior start of next session.