Activity 1
Format a duration
Write a function that accepts a number of minutes and returns text such as “1 minute” or “8 minutes”.
Hint: Use a condition to select the singular or plural word.
Swift Fundamentals
Package behavior into named, reusable units with clear inputs and outputs.
Before you begin
Learning stage
A function groups statements under a name and can receive input through parameters. Its signature describes the function name, parameter labels and types, and return type.
Swift uses argument labels to make calls read naturally. A parameter can have an external label and an internal name, or use an underscore to omit the label at the call site.
Learning stage
The function uses an argument label, a default value, and a clear return type.
func greeting(for name: String, completedLessons: Int = 0) -> String {
let lessonWord = completedLessons == 1 ? "lesson" : "lessons"
return "Hello, \(name). You completed \(completedLessons) \(lessonWord)."
}
print(greeting(for: "Ari"))
print(greeting(for: "Ari", completedLessons: 4))Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Write a function that accepts a number of minutes and returns text such as “1 minute” or “8 minutes”.
Hint: Use a condition to select the singular or plural word.
Activity 2
Write a function with correctAnswers and totalQuestions parameters that returns a Double percentage.
Hint: Convert Int values before division and decide how to handle a total of zero.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
It adds context at the call site so the function invocation reads more clearly.
QUESTION 2
Void
Void is an alias for the empty tuple ().
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: The argument label appears at the call site; the parameter name is used inside the function body.
By default Swift often uses one name for both roles, but a declaration can provide separate external and internal names. Thoughtful labels make calls read like sentences without making implementation names awkward.
Possible follow-ups
Learning stage
Functions create useful boundaries. Design signatures that state what input is required, what result is produced, and how the call should read.