Loading video player…

Why Use Functions?

00:00 Now let’s take a look at some of the reasons you would want to write a Python function. Almost every programming language supports user-written functions.

00:10 They may be called something different: subroutines, procedures, methods, subprograms, and of course, functions. They’re that useful! But why have them? Well, one reason would be for abstraction and reusability.

00:26 Suppose you’ve written a really useful section of code, something you’re probably going to want to use again. How would you reuse it? Well, the first thought might be to just copy and paste it.

00:38 A problem with that is that the names may be different in each new place you use it, so you’d have to change all of the variable names or whatever other references it used.

00:50 And then suppose you find a better way to perform that task. Now you have to go back and make that change everywhere that you’re using that particular section of code.

01:04 So, perhaps this might not be the most efficient way to reuse code.

01:11 The answer is to define a Python function to perform that task. Every time you need to use it, you simply call it. And if you need to make a change to it, you only need to change the function the one time, and then everywhere where that function is used it will use that new version. Each time you call it, you would provide the argument values needed for each particular call.

01:35 We refer to this as the abstraction of functionality, the idea of Don’t Repeat Yourself.

01:43 A second reason we might write a function is for modularity.

01:48 Modularity allows you to take a complex process and break it down into smaller steps. Suppose, for example, you had a program that was going to read data from a file, process that data in some way, and then save that process information back to another file.

02:09 So, your program might look like this: have a section of code to read the file, a section of code to process the file, and a section of code to write the file back out.

02:22 That’s a lot to look at in one particular unit. It’s going to be tough to solve problems in a program doing this many things at once. A better solution would be to take each of these segments and write a function to perform that task. Create a function to just read the data, and whatever steps are there go in that function.

02:48 Create another function to process the data, and whatever instructions were involved there would go in that function. And then another function to save that data back to a file with its related statements.

03:02 Then, your main program looks much cleaner. You simply call each of the functions. Now, this is kind of an abstract example. You might actually need arguments passed and return values saved to actually coordinate what happens from one function to the next, but this is just to give you a look at how much cleaner this program is when we break it down into these steps.

03:26 It’s easier to solve problems during the debugging stage and testing stage when your program is broken down into pieces like this.

03:36 Another reason to use functions is that they provide namespace separation.

03:42 A namespace is a region where an identifier, a variable of some kind, has meaning. This is often also referred to in some programming languages as the scope of an identifier.

03:56 A Python function creates its own namespace, so any names, any identifiers, any variables that you create within that function are limited to that particular function’s use.

04:10 So, you can always use the best variable name or the best identifier name for an object and don’t have to worry about some other function somewhere else in the program also trying to use that same name.

04:26 You’ll learn more about namespaces further along in this particular series.

04:32 Next, we will take a closer look at function calls and definitions.

Become a Member to join the conversation.