Functions
In this module, you will learn how to write and interact with functions in Swift.
StartKey Concepts
Review core concepts you need to learn to master this subject
What is a Function?
Calling a Function
Returning a Value
Multiple Parameters
Returning Multiple Values
Omitting Argument Labels
Parameters and Arguments
Implicit Return
What is a Function?
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
- 1Each 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…
- 2A 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…
- 3Now 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 …
- 4Until 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…
- 5We’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…
- 6Programs 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…
- 7The 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(…
- 8When studying Swift loops, we’ve seen underscores used in place of a variable name in a for-in loop….
- 9A 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…
- 10Swift version 5.1, released in February 2020, includes a newly supported feature for functions known as an implicit return. It exists in various other …
- 11Swift 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 …
- 12Another 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…
- 13In 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 …
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory