Key Concepts

Review core concepts you need to learn to master this subject

What is a Function?

func washCar() -> Void { print("Soap") print("Scrub") print("Rinse") print("Dry") }

A function is a named, reusable block of code responsible for a certain task. It consists of a definition that includes the func keyword, name, optional parameters, and return type as well as a body that contains the code block needed to execute its task.

Functions
Lesson 1 of 1
  1. 1
    Each program we’ve written so far, whether it tested values against conditional statements or looped for iteration, executed explicitly in the order we’ve laid out and any code that needed to be re…
  2. 2
    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 v…
  3. 3
    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 …
  4. 4
    Until this point, our functions have only printed values to the terminal. Although useful, functions are a lot more powerful when they are able to pass values out of a function using a return state…
  5. 5
    We’ve referenced the term, parameters, previously as optional input values that exist between the () in a function definition. In the previous exercise, the findAge() function operated on the value…
  6. 6
    Programs can manage more than one type of data and so can a function. We’ve previously only worked with one parameter per function, but Swift functions can accept multiple input values at a time se…
  7. 7
    The ability to write readable, succinct, and expressive code is a valuable skill to possess as a developer. Consider this function that sets the temperature of an oven: func setOvenTemperature(…
  8. 8
    When studying Swift loops, we’ve seen underscores used in place of a variable name in a for-in loop….
  9. 9
    A Swift function is intended to return a single value, but what about instances in which we need to return multiple values? We can wrap them in a collection such as an array or a [tuple](https://ww…
  10. 10
    Swift version 5.1, released in February 2020, includes a newly supported feature for functions known as an implicit return. It exists in various other …
  11. 11
    Swift supports a number of various parameters used with functions. In this exercise, we’ll explore one of them: default parameters. Default parameters have a real value assigned to a parameter …
  12. 12
    Another parameter supported by Swift is the variadic parameter. A variadic parameter is a parameter that accepts zero or more values of a certain type. A variadic parameter is useful for cases in…
  13. 13
    In our previous functions, we’ve sometimes used the values of variables as arguments. While we could return an entirely new value, we couldn’t directly affect the variable used as the argument. An …
  14. 14
    Excellent work! 👏 In this lesson, you’ve covered a major fundamental programming concept used in the majority of all programs to organize code into reusable blocks. Here’s a recap: - A function …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo