From the course: Programming Foundations: Fundamentals

Introduction to functions - Python Tutorial

From the course: Programming Foundations: Fundamentals

Introduction to functions

- Functions make programs easier to read, write, and debug. A function is a block of code packaged together with a name. They're a core feature of all programming languages, yet depending on the language, they may be called subroutines, methods, or some other name, but it's all referring to the same thing, a way to break up your code. In this course, we'll call them functions. In fact, you've already used a few functions like print to show something on the screen and input to get a response from the user. There are hundreds of other functions in Python that we've yet to explore. One key benefit of functions is that they help us to avoid writing the same lines of code again and again. You can think of it this way. I love to dance, and if I wanted to teach you my favorite dance move, the shimmy, I'd have to first walk you through all of the steps, one by one. First, you take one step to the right and stomp. Then one step to the left and stomp. Then shake those hips. That's it. Now you know how to do the shimmy. And the next time I say, "Let's shimmy", you know exactly what to do. I don't have to walk you through the steps each time. It's the same way with functions. We teach the computer how to do something one time and from then on out, we just call the function by its name and the work gets done. No need to repeat ourselves. Let's imagine this is our shimmy code. It lists out all of the steps of doing the shimmy, one by one. Without functions, we'd have to copy and paste this code again and again each time we wanted to shimmy. So let's go ahead and copy lines three through five. And then we're going to paste it down below. One and two. This makes the code so much harder to read and we're just repeating ourselves again and again. Let's run the program to check our output. We're going to right click and go down to run Python file in terminal. And we get the shimmy steps, but look. We have a bug. It's supposed to be right and stomp, not stamp. Now we need to go back to each place where we have stamp and fix it. Let's close this down. Can you imagine how difficult this would be if we had a program with hundreds or even thousands of lines of code? It makes me sad just to think about it. Functions save us from having to do this type of tedious error-prone work as programmers. Let's go ahead and make a change and we're going to create a function. We're going to call our function shimmy. Don't worry so much about the syntax. I'm going to walk you through how to create a function shortly. Just notice the benefits that we get. So we're going to delete all of this extra copy and paste. Let's fix our typo here on line number four and then we're going to call shimmy. Let's do it a few more times. Much easier to read, much cleaner. Let's save our file and then we'll run the code. Perfect, we get all the steps to the shimmy. Pretty cool, right? I'm sure you're eager to learn how to take advantage of functions in your own code. Let's get started.

Contents