Previous Lesson: Lesson 8: Commenting In Python
What are you going to do to avoid typing the exact same code lines? Ever heard of functions in Python and their significance to our code? You’ll about to learn it!
Let’s make a new file: ex6.py. Then type these in:
Before we run this, let me explain what each line does. The def is short for “define”. Here, we are defining or creating a new function. As you may have noticed, we gave our function the name hello. And whenever we name a function, we gave it an appropriate name for whatever it’s function or purpose is. In this case, we named it hello because we are saying “Hello!”
Yet there are two things that we should never forget whenever we’re defining a new function:
- A pair of parentheses (()) after the function’s name
- A colon (:) after the pair of parentheses.
Note: Without any one of these, or if one of these two are missing, our function will not function. Remember that.
Like our if elif else blocks, our editor automatically indents on the new line (on Line 2). An indentation is equal to one press on the TAB, or four presses on the SPACE.
Once we had the indentation, we’ve start giving it a few tasks. And its task or function is to print some few strings for us. Now, notice the hello() we have on Line 7. Do you have any idea why it’s even placed there? Or why it is important?
By simply typing in the name of the function with a pair of parentheses next to it, we are already “calling” for the function to execute and do its thing.
Alright. I hope everything was clear. Now, let’s run this:
You may wonder: “Do we really need the parentheses every time we call on our function? Well it’s just empty man, isn’t it?” Well, there’s a reason why it’s still used. On the next lessons, we’ll put something in there. In the meantime, try removing these parentheses and then run it. See what happens without it.
Functions in Python and their Significance
“Why do we even need functions in Python here if we could just do this instead?”
Let’s see:
The result’s the same! True, we can get rid of the function here if all we want to do is print these three simple strings on our Terminal. But it’s still important. In fact, it’s the reason that makes Python so powerful. Later, you’ll understand why.
Ever heard of “code reuse”? Code reuse is a very important part of programming in any language. When your code continuously increases, it also gets harder to maintain, especially if you’re working with the big websites.
To have successful big projects, it is essential keep have the DRY (Don’t Repeat Yourself) principle in mind instead of having WET. This bad, stinky, repetitive practice stands for “Write Everything Twice,” or “We Enjoy Typing.”
Do you ET (Enjoy Typing) the exact same lines of code over and over again? Do you wanna try it now? Let’s try this WET and realize how disgusting it is. Do not copy-paste. Just type it over and over again:
Whew! You feeling okay? Did you enjoy that? Now let’s run it pal:
If you’ve enjoyed typing all this without copy-pasting it, then I’ll have to tell you something: “Welcome to the WETClub! Where We Enjoy Typing so much!” That’s okay. I thank you for following my instructions.
On the other hand, if you felt lazy typing this over and over again, and decided to really copy-paste this, you did a good job man! The truth is, I copied and pasted these. “Welcome to the DRYAssociation!”
But, you might as well remember from second lesson that I’ve told you not to copy anything–may it be a code or an answer from your classmate’s test paper, no! And that means, you did something against the rules. But (I can’t believe I’m telling you this) for this time only (and that’s because I did that as well so), you are exempted from copy and pasting.
Functions, yeah!
But whether you’ve diligently typed all these stinking lines of code, or you lazily copied and pasted, the next method that I’m about to show you is the best. Don’t get me wrong, you did a great job, whatever method you’ve done. Yet, the next will be much proper, much neater, and a lot shorter than our previous code.
Here it is:
Let’s run it:
Good job! By the way, whatever your operating system is, whether if it is Mac or Windows our code works totally fine.
So, that’s it with functions in Python and their significance. If you didn’t get whatever it is we just did with functions, don’t you worry ’bout a thing. You’ll understand this as you keep going on to the next lessons. You’ll get better; maybe even a lot better than me. So see you on the next lesson.