Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Why Functions Are a Good Idea

00:00 In the last part, Martin started the coding challenge, and he wrote everything plainly in the file. In this part, he will use functions to structure the code and also explain why functions are a good idea. Okay. I don’t want to print the character.

00:16 What I want to do is double each of them. So I already have access to each of the characters, as we just saw in the print statement. So now I can just add to this, like overwrite it, and I want the character double, so I’m just going to multiply it by two. This is something you can do.

00:36 And sometimes it also helps if you just jump into a little experimental session and figure out, for example, can I do something like that in Python? Yep.

00:45 That seems to work. I can just use the multiplication operator (*) on a string, and then I get the string doubled. Yeah. So I like to have the terminal also close by, where I can just run quick experiments basically. It’s a little bit like a sketch pad where you are trying out an idea and see if it actually works. So right now you’re still having—I was just like—you’re still having the pass, but you don’t need to anymore because you’re inside of the loop.

01:14 So that should be it, really. I need to also make sure that we also see the value at the end, so I’m going to print it out.

01:22 And now if I run this double.py, I get the word, the original word that was input, and everything doubled up. Looks good. Perfect. Looks just like what you wanted me to write here. Yeah, so we are almost done, but not yet.

01:39 Yeah. So what we’re missing, so right now you’re just like plainly writing it out from top to bottom in a file. What’s the advantage of actually putting this in a function now?

01:48 Like, couldn’t we just leave it like this? Yeah. It’s just not, it’s not very reusable. So in this case, first of all, you know, like the word is hard-coded in this file. So if you wanted a different word, you’d have to go in here and change it. For example—and oh, that’s gonna be fun—let’s look at it with this double p at the end.

02:13 Look at that. That’s nice. I should change my name to this, to the screen name. Much more interesting than mine doubled. Okay. We’ll stick with Philipp. So I’d have to go into the file and change this, and I can’t really reuse this anywhere else. And also, I can’t really write any tests for this easily, you know.

02:33 If you package your code up into functions, it makes it much more reusable for different things—for testing, but also for using it in other files. So yeah, definitely a good suggestion here: create a function that accepts a string. Yeah.

02:47 So I could rework this. Let’s do it. Oops, frg.

02:52 It’s just my hands wrong and keep writing. Okay. Yeah. I really liked your explanation because an instruction like this could also be part of a coding interview, and your short answer could have been like, “Well, actually it’s in the instructions, so that’s why I’m creating a function.” But usually there is a why behind something, why it might make sense to do something and not just follow instructions blindly, but also knowing why it might make sense in the long run.

03:23 Yeah. If you’re in a good interview situation, then your interviewer is going to actually value if you explain your thought process of building something, you know, because that’s often much more valuable that you have someone who actually can communicate what they’re doing and why they’re doing it in a certain way.

03:38 For a work environment, that’s super helpful. Absolutely. Okay. So I’m going to, I’m writing this function. I’m going to call it double_characters(),

03:50 and it takes a word as input. And then essentially I can just indent this. You know, the word is already the variable I’ve been using, so this is going to be the attribute that’s passed in here and then used down there. And then instead of printing it, I want to return so that it’s actually reusable, that there’s an output to this function.

04:12 That’s really good. And now I would write this if magic: if __name__ == "__". So I’ve seen this plenty of times. What does it do?

04:25 Why is it recommended to add, like, this if __name__ == "__main__" when you want to run a file? So if I don’t have this, all right, and I would run this file now, obviously I’m not calling the function, so nothing’s going to happen.

04:43 But if you’d be importing this file somewhere else, one possibility would be, right, I put a function call here and say, pass in the word. Right now. If you run the file—oops, that’s still not printing it.

05:01 Now, if you run the file— Yeah, because so far you were just returning it, but there wasn’t a print statement to actually show it. Correct, yeah. A print() function. Yeah, because I removed the print() function here, I had to add it down there to actually get output back to the console, yeah.

05:17 So I could do this, right? But now if I want to reuse this function in a different module, for example—and that’s, reusability with functions is a big thing—so maybe, I don’t know, you write this other script and you want to use the double_characters() function that you have defined in this script.

05:33 So you go over and you say, from double import double_characters, and if you had this—now, let’s actually try it out, right—so let’s make a new file in here.

05:45 Before we let Martin try out importing the code into another file, let’s quickly recap what happened so far. Martin basically solved the challenge. He created a function that takes a string and returns a string with double the characters. The most important line here is the one in the for loop.

06:04 There, he adds the duplicated character where he is in the word right now to the double string. So with every iteration of the loop, one character gets duplicated and added to the string, which is then, in the end, returned from the function.

06:23 Martin also already said why he thinks a function is a good idea. By using a function, his code is easier to maintain. Functions are reusable, and there are less side effects during import.

06:37 And that’s what he’s about to show now.

Become a Member to join the conversation.