Abstraction is when we take our complex code and simplify it by breaking it into smaller chunks.
Abstraction is when we take our complex code and simplify it by breaking it into smaller chunks. This is what functions do for us. Functions let us wrap up parts of our code into bite-sized containers that do one specific thing. Then we summon that function later when we want to run the code that lives in the function. We use functions for clarity, organization, and for ease of understanding.
In this module, you’ll learn how to write your own functions and how to manipulate properties inside of your functions, and how to extract values from those functions using return. You’ll also learn how to use function parameters so your functions can have flexibility and responsiveness in your code base. In addition, you’ll learn how to craft clear and easy-to-read functions using Swift’s parameter labels. These will make your functions read much like a sentence instead of something difficult and obscure.
Learning Objectives
- Recognize the structure of a function
- Understand how functions abstract your code
- Explain customizing functions
Skills you’ll gain
Function ModuleFunctional ProgrammingStored FunctionsSwift (Programming Language)User Defined FunctionsUtility FunctionsWhat You'll Learn
- Recognize the structure of a function in Swift
- Write your own functions to abstract and organize your code
- Return and extract values from functions using return
- Use function parameters to add flexibility and responsiveness
- Apply Swift parameter labels to write clear, readable functions
Key Takeaways
- Abstraction means simplifying complex code by breaking it into smaller chunks, which is what functions do.
- Functions wrap parts of your code into bite-sized containers that each do one specific thing, then run when called.
- Functions are used for clarity, organization, and ease of understanding.
- Function parameters give functions flexibility and responsiveness within your code base.
- Swift's parameter labels make functions read much like a sentence rather than something difficult and obscure.
Frequently Asked Questions
What will I learn in this module?
You will learn how to write your own functions, how to manipulate properties inside your functions, how to extract values using return, how to use function parameters for flexibility, and how to craft clear functions using Swift's parameter labels.
What lessons does this course include?
The course covers three lessons: Introducing Functions, Returning Values from Functions, and Parameter Names and Labels.
Why are functions useful in Swift?
Functions let you abstract complex code by breaking it into smaller, reusable chunks that each do one specific thing, which you can call later, providing clarity, organization, and ease of understanding.
What skills does this course cover?
It covers function modules, functional programming, stored functions, the Swift programming language, user-defined functions, and utility functions.
Transcript
Show transcript (free preview lesson)
Transcript of the free preview lesson. Remaining lessons unlock with the full course.
Hello. My name is Mark Nair. And in these lessons, you'll learn about functions, returning values from functions, and using parameter names and labels. Functions are a superpower. They let us take a lot of code, wrap it together, and then call that code when we need it. So instead of having lines of code all over the place just doing the same thing over and over again, we could take out those little chunks, pop it into a small package, and then summon that package, run that little code, and then be done. Functions are a great way of organizing your code base, a great way of thinking about your project, a great way of coming to the right answers to solve the problems that you're facing. Let's take a look at a basic function. First of all, a function wraps itself in a keyword. In Swift, we use the keyword func. Now after the keyword, we write the name of the function. This function, all it will do is just print out Hello. So I'm gonna call it sayHello. Now functions require two parentheses after the name of the function. The reason for this is because we will put information in the parentheses later to help us customize our functions. Just keep in mind right now that you write func, the name of the function, whatever you're calling it, and two parentheses, just like that. Then we end it with the curly brace. So this is the skeletal outline of the function. This is how functions look. There'll be many other things inside of that look, but this is the basic look right here. So func, the name of the function, parentheses, the curly braces. Now, in between the curly braces is where our code will live, which is very similar. The in between curly braces on looping and in between curly braces on many other constructions in Swift. Now what we wanna do is just print Hello!. Hello!, just like that. So this function will just, when we call, it's called calling the function, when we call the function, the only thing that will happen is it prints Hello!. If I run my code right now, nothing happens. Nothing happens because even though we have said print the string Hello!, the function is actually just waiting for us to summon it. It's kinda like a genie in a bottle, but we haven't really rubbed the bottle yet, we haven't summon the genie, so let's summon the genie. The way we do that is calling the function, we just say the name of the function, sayHello. Now if I run this, Hello!, down here at the bottom. And that is the gist of a function. Now this might look kind of similar to different things we've done before with a good exception, by that I mean the curly braces here, the print statement here. The difference is we're using the keyword func, we're starting, it's camel case, so small case, we're not making a new type, in these parentheses right here. So let's take a look at another example. Let's say I wanna figure out the area of a room. This is a classic example. We're gonna use, we're gonna go all old schooling in this. Previously, we'd do something like this. All right. I have on line 7, right here, length = 12, then width = 10 on line 8, then I'm doing the calculation on line 9, and then I'm printing out the result. Running this gives us this. Okay, works great. But if we wanted to run this again, we wanted to have a different length than width. We'd wanna have a different room. We're analyzing different things. So, you know, we have an app. You analyze your living room, then your bedroom, then another room, and then a stadium. We don't wanna write let stadium length and stadium width and bedroom length and living room length and garage length, all that stuff. Instead, let's make this much more efficient. It'll do that with function. Now, remember function starts with a keyword func, func. Then the name of the function. Let's call this function calculateArea. Remember, two parentheses, open, closed parentheses, curly brace. Now what I'm going to do is take this code right here that I've already written. I'm gonna cut that, and I'm gonna pop that into the function body. When I say the body, I mean this area here between the curly braces. All right, now I have this function calculateArea. I'm gonna comment out our Hello! function so we don't have a lot of things going on out here, and I'm gonna run this. Nothing happens, and nothing happens because we haven't called the function. Let's call it. Now, let's run it. There it is. The area of the room is 120 square meters. So just keep in mind that functions are really very useful and you will use them all the time in Swift. You'll use it for something small like this, something easy, and then use 'em for very sophisticated things. So speaking of sophisticated things, let's take a look at what happens inside of these parentheses and what happens to information and the function that you need to use, but you just don't wanna print it all the time, 'cause that's what this function does, it just prints. So thanks for watching. In the next lesson, I'll show you how to return a value from a function.
Learn on the Go
Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.