Functions are essential for grouping blocks of tasks together. In this video, create a few simple functions to get familiar with the new syntax.
- [Narrator] Now that we've got our control flow basics down, let's move on to writing functions. I'm going to go into my Adventureswift_Working folder, and open up chapter six. Functions are blocks of code that we can define and call by name to perform whatever tasks we've coded into them. Let's write our first function here, and we'll keep it super simple. We start with the func keyword, and then you name it.
So we are going to name our function saluteHunter. Now you need to add an open and close parenthesis, and open and close curly braces. Now we will get into this in a bit, but those parenthesis are where our parameters would go if we had any. You can think of parameters as inputs and return values as outputs. So in our saluteHunter, we are just going to have a print statement. This is going to say, 'Hello there intrepid Hunter'.
To call any function, you just use it's name. So here saluteHunter and open and close parenthesis. The next code will usually complete that for you. So here in our debug log we've got, 'Hello there intrepid Hunter'. Alright, perfect. That was kind of a boring function, so let's write one that has a return type, or an output. Again, func, we'll call this findNearestHunter, and again no input parameters, but this time we are going to have a return type.
This new syntax with a dash and a right arrow is how we define return types. So after this, we specify what we're expect the function to return. So in this case, we are going to be looking for a string. In here, I'm going to use the return keyword, and I'm just going to give it a string literal, 'Argus'. Now, since our findNearestHunter function has a return type, we can capture this is a variable. I'm going to call this nearMe, and I'm going to set it equal to the return of findNearestHunter.
So here in our right hand inspector we can see that nearMe is now equal to Argus. Now for the last piece of the puzzle, let's define a function with a return type and an input parameter. Let's call this equipItems. Open and close parenthesis, we'll fill this in, in a minute. And then we will have our return type be a boolean. Now input parameters need to have a name, and a type. So, here I'm going to say armor is going to be a type string, and that's it for now.
Just for clarity, This function is saying we're going to take in a string, and we are going to return a bool. Now, any input parameters that we pass into a function can be used in their code block. So, for instance, we'll can print here and we use string interpolation, we'll say armor. Successfully equipped. And don't forget that we have to return a boolean value so we're going to return true.
Let's call our new function equipItems, and here the X code in TeleSense is going to ask us for our armor string. Here I'm going to pass in Demon Vest as a string, and wait for it to compile. Here we go, in our debug log we get Demon vest successfully equipped and our true boolean value is returned from the function.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs