Have you ever wondered how when you turn the dial or press a different button on your washing machine for a specific wash, it just knows what to do? You select sports wash, and the machine runs a wash cycle at the same temperature every time. Or you turn the dial to synthetics, and the machine runs a wash for the exact same length of time each time. Functions work just like this. Running the same set of stored instructions repeatedly without requiring you to adjust the settings every time. Functions provide you with the flexibility to create a set of instructions that are convenient to use in your code. In this video, you will learn about simple functions in Swift. You will explore how functions are structured and how to declare a function in your code with the use of parameters. You will discover how to call or invoke a function with the use of parameters, and then explore function return types. Finally, you will learn about using tuples to return multiple values and functions. You've already used a Swift function in this course, such as the print function, which is declared in the Swift library. Recall to use this, you execute its name followed by a pair of parenthesis, and then you type a value such as a string like "Hello world." Let's start with what is a function and what does it do. One of the basic principles of programming can be summed with the acronym DRY. In other words, don't repeat yourself, and it's thanks to functions that you can avoid repetition. With functions, you can take several lines of code that perform a set of related actions, and then group them together under a single label. Then when you need to run the code that you've saved, you just invoke or call the function. You can run the code as many times as you want. As functions are only defined ones, they allow for the reuse of code. For example, suppose you need to create some code that adds two numbers and prints out the result, the process could be as follows. First, create two variables, a and b, and assign each a number value. Then create another variable, c, which holds the value of a and b added together. Finally, you output the value of c. This piece of code works and adds two numbers together, but what if you want to execute this code more than once? Well, you can do this by placing this block of code inside a function. To create or declare a function, you type the keyword, F-U-N-C or func, then a space, and then the name of the function. You can name the function nearly anything you want. This name should be unique as each function usually performs a specific task, the name should help to identify and describe what the function does. In this example, it's named addTwoNums. After the function name, place a left and right parenthesis. The code to be executed will then be placed in between the left and right curly braces, and this is known as the body of the function. It's important to remember that a function declaration on its own doesn't execute the code. It's just specifying what operations should be performed once the function is executed. To run the function's code, you need to call or invoke the function, and this is achieved by typing the function name followed by the opening and closing parenthesis. Instead of saying run a function, developers may say that they call a function or invoke a function, but don't worry, all of these terms mean the same thing: to execute the code inside the function's body. Next. Let's explore another major advantage of functions, the use of values. Did you notice how the addTwoNums example was just adding the numbers 10 and 20? These numbers are our chosen values for this function, but this is just one example of the use of values. With functions, you can send it any values you want, and you do this by defining something called input parameters when you declare a function. These parameters allow you to pass one or more values to the function when called. These values are known as arguments. Take, for example, the addTwoNums function created earlier. The function accepted no arguments and no parameters were defined. Instead, variables were used to produce results. While you can use variables this way, there are limitations. For example, as variables a and b have specific values, the function always outputs the same result. So let's change this function to make it more flexible using input parameters and passing arguments. It's important to remember that functions aren't required to define input parameters. Not all functions require these. In order to pass values to the function, you need to create function parameters in the function definition. Parameters allow you to set changeable values on each function call. The values passed to the function are called arguments, and these are local variables that will hold the value of the passed data. Notice that if you have even more than one, then these are separated by a comma. When parameters you must declare the type. For example, Int. By setting a and b as parameters, you can pass in any two numbers as input to the function. Then the function will perform the calculation to add the numbers. Parameters allow you to set changeable values on each function call, and functions with parameters can accept any kind of value and results in clean, more efficient code. Building functions this way allows you to write flexible functions that can be reused with various values as many times as you need. For example, by passing values as arguments to the function addTwoNums, you can call it as many times as you want. As long as you send it two values, it will add them together and output the result. Now you know how to create functions and pass values to them. Let's now explore how to return an argument from a function. A function can return a single value, and you specify what type of value the function will return. This is known as its return type. Then in the function body, you use the return keyword to return the final value, and this must be the last command that the function executes. It's also possible to return multiple values from a function. To do this, you need to use tuples. You may recall learning about tuples which enable you to create and pass around groupings of values. By using tuples, you can return multiple values from a function as the values are grouped into a single compound value. Each value can be of any type and do not have to be of the same type to each other. For example, suppose you need to create a function that will accept two numbers and then output them in the order of the minimum and maximum, you need to calculate which number is the minimum and which is the maximum, and then return the result. In this lesson, you learned about functions in Swift. You're now familiar with the advantages of using functions, declaring a function with parameters, and calling a function with arguments. You also learned how to return values from functions. Working with functions is common in day-to-day development and is one of the core skills a developer needs to master as they help you write more efficient, reusable code.