Skip to Content
Learn
Functions
Calling a Function

Now that we’ve practiced defining a function, let’s learn about calling a function to execute the code within its body.

The process of executing the code inside the body of a function is known as calling it. To call a function in Swift, type out its name followed by parentheses ().

Take the following makeSushiRoll() function which holds step by step instructions on how to make a sushi roll:

func makeSushiRoll() { print("First, spread rice on Nori") print("Next, add fillings") print("Carefully roll the sushi") print("Cut the sushi") print("Serve 🍣") }

To call our function, we must type out the function’s name followed by a pair of parenthesis:

makeSushiRoll()

Calling the function will execute the print() statements within the body and result in the following output:

First, spread rice on Nori Next, add fillings Carefully roll the sushi Cut the sushi Serve 🍣

Now it’s your turn to call a function!

Instructions

1.

Assume we’re writing a program that holds our favorite recipe. In Recipe.swift, define a function, mySpecialRecipe(), that will hold the instructions for a recipe of your choice.

  • mySpecialRecipe() must not return any values.
  • mySpecialRecipe() should contain print() statements with the instructions for your recipe.
2.

Below the function definition, call the function and see your recipe in the terminal.

Folder Icon

Take this course for free

Already have an account?