Working with functions helps programmers to write better code. After watching this video, you will be able to create your own functions in Python.
- To better understand how functions work, let's create one. We're going to work with an empty file over here in VS Code. Here on line number one, we're going to create our first function. To do that, we're going to use the define statement. That starts with the def keyword, which is short for define. This lets Python know that we are ready to create a function. Next, we need to give our function a unique name. We're going to call this one say_hello. We have to give our function a unique name, just like variables. We could name it anything we want, but we have to make sure we respect the rules of the Python language. For example, I could name this function funky town, but that might confuse me or the next developer who reads my quote. So we're going to stick with say_hello for now. The next thing we need to do is add an opening and closing parentheses, and then a colon. This is the requirement of Python syntax for creating a function, similar to what we had for the if-else statement. Once we hit Enter, the IDE automatically indents this line of code, because it's trying to help us out. It knows that functions have to be indented. Now, where have we seen this before? That's right. We do the same thing for our if statements. We often call the code block that belongs to a function its body, so when you hear function body, that's simply another way of referring to the code that the function is going to execute. So let's move back over to our code. In order to get the hang of this syntax, we're only going to have one line inside of our function's body, and it's going to be a print statement that says hello, friends. This is the say_hello function after all. With that done, we can save our file and let's run it. Uh-oh, we don't get any output. Can you guess why? Think back to when I taught you the steps to my favorite dance move, the shimmy. How could I get you to dance with me? I'd have to call out, let's shimmy, and then you'd know it's your time to shine. Similarly, creating a function means that we taught the computer how to do some work, but in order to have that work done, we need to call the function first. In Python, you call a function by using its name, followed by a set of open and close parentheses. Let's do that now. Right underneath where we have line number two, we're going to hit Enter, and by default, the IDE tries to help us out and it automatically indents this line of code. But that's not what we want. We're done with this function. So we'll hit Enter one more time and delete this indentation, so that it's clear say_hello is complete, and finally, in order to call the function, we're going to type its name, say_hello and open and close parentheses. Let's save it and run our file again. This time, we get the hello friends message. Pretty cool. In fact, what if we wanted to say hello a few more times? We can copy line number four and paste it right underneath on line five and line number six. We'll save and we'll run it one final time. And there we go. Hello friends, hello friends, hello friends. Now you know how to define and call your very own functions. While this may seem basic, soon you'll learn how powerful it is to incorporate functions in your own programs.
- Writing source code
- Basic statements and expressions
- Working with numbers and strings
- Writing conditional code
- Increasing your efficiency with functions
- Working with comments
- Making decisions in code
- Troubleshooting errors
- Learning about other languages