Learn
Functions
Defining a Function

A function consists of many moving parts, so let’s first get familiar with its core - a definition.

A function definition consists of a function’s name, optional input values, and the type of value the function returns. Following the definition is a code block or body that contains the function’s task.

Here’s the set up:

func functionName() -> returnType {
 // function's task goes here
} 
  • The func keyword indicates the beginning of a function. It is followed by a name in camelCase format that describes the task the function performs. It’s best practice to give your functions a descriptive yet concise name.

  • Following the functionName is a pair of ( ) that can hold optional input values known as parameters. (More on parameters later in the lesson!)

  • Following the parentheses is a return arrow (->) with the type of data the function gives back, or returns. If a function does not return a value, one of the following two syntaxes can be used:
    1: func functionName() { where the -> and type are omitted entirely.
    2: func functionName() -> Void { where Void is listed as the type.

  • Lastly, the returnType is followed by a code block that holds the code for the function’s task.

Using the pseudocode above as a reference, we’re going to create a simple function without a return statement or parameters. As we continue through this lesson, we’ll learn more about these different parts and concepts.

Here’s a function that greets a user:

func greeting() -> Void { print("Hey there!") print("How are you doing today?") }

The function above can be referenced by its name and a pair of parentheses: greeting(). It does not return any values, denoted by the keyword Void, and only contains two print statements in its body. Now every time we’d like to greet a user, we can use the greeting() function.

Note: Pasting this code into the editor and clicking “Run” will result in an empty output terminal. The print() statements within the function will not execute since our function hasn’t been used. We will explore this further in the next exercise; for now, let’s practice defining a function.

Instructions

1.

Two of the most common NYC attractions include the Empire State Building and Times Square. In Directions.swift, we’ll write a function that prints the directions via subway from the Empire State Building to Times Square.

First, define a function, directionsToTimesSq() that will not return any values.

Leave the body of the function empty for now. We will populate it with code in the next step.

2.

Within the body of the function, use 4 print() statements to output the following directions:

  • "Walk 4 mins to 34th St Herald Square train station."
  • "Take the Northbound N, Q, R, or W train 1 stop."
  • "Get off the Times Square 42nd Street stop."
  • "Take lots of pictures! 📸"

Remember, you shouldn’t see any output in the terminal at this point.

Folder Icon

Take this course for free

Already have an account?