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.
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
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.
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.
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.
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: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.
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.
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.
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.
Now, if you run the file— Yeah, because so far you were just returning it, but there wasn’t 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.
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.
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
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.
Become a Member to join the conversation.