Skip to content
Prepare to Grow

Swift Fundamentals

Functions and Parameters

Package behavior into named, reusable units with clear inputs and outputs.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Declare and call Swift functions.
  • Use parameter labels and return types.
  • Provide default parameter values.
  • Design small functions with one clear responsibility.
01

Learning stage

Concept Explanation

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.

  • -> introduces a return type.
  • return sends a result to the caller.
  • Default values make selected arguments optional at the call site.
  • Functions are values and can be passed to other functions.
02

Learning stage

Syntax & Examples

The function uses an argument label, a default value, and a clear return type.

Greeting.swiftswift
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))
The default is used in the first call; the second call supplies 4.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Transform API responses into display models.
  • Validate user input and return a meaningful result.
  • Encapsulate formatting, calculations, and feature decisions behind descriptive names.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Creating functions that perform several unrelated jobs.
  • Choosing argument labels that make calls repetitive or unclear.
  • Using global state instead of explicit parameters and results.
  • Ignoring how a function reports failure or absence.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

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.

Activity 2

Create a score function

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.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

What does an argument label improve?

Reveal answer

It adds context at the call site so the function invocation reads more clearly.

QUESTION 2

What return type is inferred for a function with no returned value?

  1. A.Void
  2. B.nil
  3. C.Any
  4. D.Never
Reveal answer

Void

Void is an alias for the empty tuple ().

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

What is the difference between an argument label and a parameter name?

Short answer: The argument label appears at the call site; the parameter name is used inside the function body.

Explore the detailed answer

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

  • • How do you omit an argument label?
  • • When are default parameters useful?
08

Learning stage

Key Takeaways

Functions create useful boundaries. Design signatures that state what input is required, what result is produced, and how the call should read.

  • Function signatures are part of API design.
  • Labels provide call-site context.
  • Default values should represent sensible common behavior.
  • Small functions are easier to understand and test.