Programming Assignment
University of The People
CS 4405-01: Mobile Applications
Instructor: Augustina Agor
23rd July 2025
Part 1 screenshots:
Part 2: siftBeans(fromGroceryList:)) and General Functions Explanation
For this part of the assignment, I provide a Swift function named
siftBeans(fromGroceryList:) that filters bean-related items from a grocery list
represented as an array of strings. Following that, I explain the concept of functions in Swift,
detailing different types—functions without parameters, functions with multiple parameters,
functions without return values, and functions with multiple return values—each
accompanied by an example.
1. The siftBeans Funtion
The task requires a function that accepts an array of strings (a grocery list) and
returns a new array containing only the items with "beans" in their name. Here is the
implementation:
Explanation
o Parameter: The function takes fromGroceryList, an array of strings, as
input.
o Filtering: The filter method creates a new array by including only elements
that satisfy the condition in the closure (Apple Inc., 2023). Here, $0 represents
each string in the array.
o String Operation: $0.lowercased() converts the string to lowercase, and
contains("beans") checks if "beans" is present, ensuring case-insensitive
matching (e.g., "Green Beans" or "black beans").
o Return: The function returns the filtered array.
Example Usage
Using the provided grocery list in the assignment prompt:
2. Functions in Swift
Functions in Swift are self-contained blocks of code designed to perform specific
tasks, often taking inputs (parameters) and producing outputs (return values). They
enhance code reusability and organization (Apple Inc., 2023). Below, I describe four
types of functions with examples.
Functions without Parameters
These functions take no inputs and are useful for tasks independent of
external data.
The example below shows a function that simply prints a greeting without
requiring any parameters:
Functions with Multiple Parameters
These functions accept multiple inputs, each with a name and type, to
perform operations requiring several pieces of data.
In the example below, the function addNumbers takes two integers and
returns their sum:
Functions without Return Values
These functions perform actions but do not return a value, often used for
side effects like printing. They implicitly return Void. Example:
Functions with Multiple Return Values
These functions return multiple values using tuples, allowing complex
outputs in a single return statement. Example:
References
Apple Inc. (2023). The Swift Programming Language (Swift 5.9). Retrieved from
https://docs.swift.org/swift-book/
Sundell, J. (2023). Understanding Swift’s filter function. Retrieved from
https://www.swiftbysundell.com/articles/understanding-swifts-filter-function/