Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 5: Functions & Named Types

36. Functions & Return

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 35. Introduction to Functions Next episode: 37. Challenge: Functions

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 36. Functions & Return

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

So, if you followed along with the first part of this course and built the Bullseye app. You might remember writing functions like amount off or slider value rounded. Both of those methods returned a value. So when a function or a method returns a value, once you call it function, that value can be stored or used in place. For example, points for current round stored the result of amount off that you could use in an if statement and amount off subtracted, the result of slider value added from a target value. To make the print pass status function return something, I first need to add the return token after the parentheses. The return token is just a dash and a greater than sign put together. And then I'll add the type of the value I want to return, in this case I just want a Boolean. So that's step one. Step two is to use a return statement, inside of the function body. So I'll just use the word return, followed by whatever value I want to return. In this case, that's great, greater than equal to lowest pass. So, the type of the value return must match the type specified in the function declaration. So in this case, I just want the result of this expression, which is a Bool. In previous versions of swift, the return keyword was required but now you only need it if the body of your function is longer than one line. If the body for function is one line long, like yours is, you can leave off the return. And the result of that line will be implicitly returned for you. So now that this function returns a Boolean, instead of printing something to the console, I think it needs a new name. So most of the code you've written in this course has represented nouns like students, pets, grades, pastries, or temperatures. Functions represent tasks or actions. They're bits of code that do something. So that action can be represented by using a verb in the function name. So print highest grade, print's the highest grade to the console. In Bullseye, start new game, starts a new game. But if you recall the method examples from Bullseye, amount off and points for current round, are named for the values they return. When you call those methods, you get the amount off or the points for the current round. So naming functions or methods for the values they return, is a fairly common convention. Now another common convention is to add a verb, like get, or make or calculate to function names, like get points for current round or calculate amount off. So if you're working with a development team, they probably have some naming conventions for you to follow. But if you're your own boss, how you name your functions is another stylistic decision you're going to have to make. Now any other case, staying consistent in your naming will make it much easier to read about and reason about your code in the future. To give you some variety though, I'll rename this function in the verb style, get pass status. Now I can call it function and store the result. So I'll use these tuples at the top of playground, for the grades. (keyboard clicking) So I could also check to see if the whole class passed, using the return values directly in an expression. (keyboard clicking) Oh man, that Chris character, he's dragging the whole class down. Anyway, as you're learning about returning values, there's one more use of the return keyword you should know about. while we're at it, I'll also show you one more way to deal with optionals and how to store the type of a tuple. So take a look at these two tuples, representing our students. I've just left the pet value out of Sam's tuple, but what if I want to express that Sam doesn't have a pet here. If I just add a pet value, instead of to know. The compiler has no idea what optional type this is meant to be. So hopefully you remember from your exploration of optionals, that the solution to that, is to explicitly provide a type for the value. The type annotation for a compound type, like a tuple, looks basically identical to a parameter list. You add that whole type annotation, in exactly the same place. You add a named type annotation. (keyboard clicking) So there's the optional string. I want to represent a pet, but that's a lot to type, especially considering I want to use the same type information for our other student. Now, you can store this compound type for reuse, using something called a type alias. Now, a type alias is sort of a lightweight type. It lets you a name to a compound type or give an alternate name to an existing name type. So we'll start with keyword type alias and then the name you want to use and a single equal sign. So then I'm going to copy the type annotation from Sam and paste it at the end. So now I can say that both Chris and Sam are students like this. (keyboard clicking) I can also write a function with a student parameter. So I'll write a function that orders a color for a student's pet, but only if that student actually has a pet. (keyboard clicking) So in this next line, I'll do two new things. First, I want to make sure the student passed into the function actually has a pet. So you've already seen how to do that with if let binding and Neal coalescing. And there's one more way to bind an optional value and that's guard let. Now, guard let starts out much like if let. (keyboard clicking) But the difference comes at the end. With guard, you always need to provide an else clause. So I've reached the second new thing, which is another way to use the return keyword. So you can use the return keyword, without actually returning a value. So return here will exit the function immediately and return to the executing code right after the function call. And that's exactly what I want. If a student doesn't have a pet, I don't want to continue executing code in this function. But if they do have a pet, that value has been bound to the local pet constant. And I can use it to pretend to order personalized color with a print statement. (keyboard clicking) So now when I call the function and I pass in Chris. (keyboard clicking) I get a shiny new color for mango, but if we use the same function with Sam, (keyboard clicking) Nothing happens. Now next up, I have a challenge to help you try out everything you've learned about functions. After that, we'll revisit the student type alias and turn it into your first named type.