Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Functions, or subroutines, are a very common form of reusable code, so let's look at how functions are done in Python. We'll make a working copy of syntax.py. We'll call this syntax-functions.py. Go ahead and open our working copy. We'll notice, of course, that main here is a function. And the purpose of that is not so much reusability as it is modularity in allowing us to create more functions, and be able to run them from main.
So let's go ahead and create a function here. And I'll just call it func. And we'll print out a range of numbers. And in my print statement I am going to say end= ' '. And that way instead of putting each number on a separate line, we'll put them all in the same line, separated by spaces.
And I'll put another print after it, an empty one to print a new line. And so I'll save that, and I'll call the function from in main. And we'll just call it instead of this print function here, we'll just say func. And that's all we need to do to call our function. So we'll save it and run. And we see we are printing out this sequence of 10 numbers. And this is, in fact, how the range function works. It prints out everything not including the number of the range.
So it's 10 numbers staring at 0. Now if I wanted to call the function again, I would just say func again, and I can do that several times. And each time it will run this same block of code. So I save it and I run it, and that's how a function works. So let's look at how we define the function. The def keyword is for defining the function. That means that what comes next is going to be the name of the function, so there is the name, and then there is a set of parentheses where we would put arguments, or parameters.
And the colon says that this is going to be followed by a block of code, or a suite of codes, as it's called in Python. And then we have the suite of code itself. The suite of code is indented under the definition of the function, and in this case of course, we're using the traditional four spaces of indenting, like that. Inside the parentheses we could put arguments. So for example, if I put an argument called a there, then I could say a, 10.
So it would start at the number a in its range here, and so here I could use a 1, and here I could use a 3, and here I could use a 5. And it would use that as an argument to the range function. What that does is it defines a starting point for the range. And so if I save that and run it, then we see we have one that starts with 1, one that starts with 3, and one that starts with 5. And so this argument in the parentheses is used inside the function in this way.
We can give the argument a default value by saying a=0. Well, let's give it something different so we can see. Let's say a=7. And if in this middle one I don't put an argument, then that will be the default argument that's used. It'll default to 7. So if I save this and run it, you see that the middle one starts at 7. So there are a lot of other things we can do with functions and with function arguments, and we'll cover those later in our chapter on functions. But this is, in a nutshell, how you define a function in Python, and how you call and use that function, and how you can use arguments when you call and use that function.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99439 Viewers
56 Video lessons · 112667 Viewers
71 Video lessons · 81493 Viewers
131 Video lessons · 39121 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.