Previous Lesson: Lesson 9: Functions in Python and Their Significance

We’ll be making things more interesting in this lesson. ‘Cause we’ll be creating functions with parameters and arguments in Python.

Let’s create a new file: ex7.py. I’ll be on a Mac for this lesson, so things may be a little different, especially on the Terminal. But the results will just be the same–whatever. Type this in:

Do you still remember how functions work? When we’ve defined a new function on Line 1, we now had a parameter inside its parentheses. After a few lines, when we called our hello function on Line 5, we also had something inside its parentheses. It’s called an argument. The two of these seemed to be connected; they really are. How does it work? We’ll talk about this in a sec. For now, let’s run this:

Nice! What just happened?

A Parameter and an Argument

As I have already revealed to you, the two of these are related. The parameter is like a variable. While the argument is like the value assigned to that same variable. But unlike variables, we can have different values or arguments for our parameter. Watch this:

Have you typed this? Of course you should! Now let’s run it:

Now what happened this time? Since we’ve called our function five times, strings have been printed five times. And since these functions have five different parameters within their corresponding parentheses, there were five different thestrings. You got it? Read this over and over again in case you still don’t. Good.

Two Parameters and Two Arguments

Now let’s try having two parameters and two arguments on our code. Type these in:

Let’s run it:

Oops! We’ve been doing this since we’ve started running our code. What I forgot to tell you is that in Mac, in order for our Terminal to know what Python version we’re going to use (in our case Python 3), we need to say python3 ex7.py. Let’s do it. This should work this time:

Alright! Just as what we have learned previously, you can think of parameters as variables. And we feed to them their values once we’ve called on the function with arguments.

And since name in our string on Line 7 and not myname, the string would not get, or it would ignore the “Zedd”. The same also works for the age. Instead, it would get the value or arguments of the parameters name and age from Line 9. And these arguments are Emmaand 28. That’s why Emma and 28 got printed, not Zedd and 16.

Now let’s have a little twist. But I know you’ll still get this:

Let’s run it:

Tada! Now what is the flow of our code here? Simple. We’ve assigned the variable myname to “Zedd” and we’ve assigned the variable myage to 16. And then we used these variables as our arguments as we call on our function. Which means that our arguments are “Zedd” and 16.

On the other hand, we have name and age (both inside our string) as our parameters, its corresponding arguments are “Zedd” and 16. Which was then printed on our Terminal.

So that means, if we change the value of our variables here on Line 3 and Line 4:

We would also have a different outcome:

Cool! Play around with this one, I tell you.

Two Parameters Should Have Two Arguments

Why is it important to have two for two? On our code, I’ll get rid of myage:

Now notice what our Terminal will say:

See? It says that we were missing 1 required positional argument: which is, expectedly, ‘age’. That’s the effect of missing out an argument that is expected by our function.

Parameters with no Arguments

Now make your code look exactly like this:

There are no arguments, yes? And yet we have two parameters, no? Let’s see what happens:

Nice! If we or a user ever forgot to enter his name and age when asked, setting default values is very useful. Because instead of spitting back an error, this automatically fills in the empty requirements.

Bonus!

Let’s do something that will cook your noodles using functions with parameters and arguments in Python. You remember our old buddies? The if elif else?

Take note that we didn’t enter our name and age as arguments. See what it says:

Alright. Now it asks for our correct name and age because we know that ma’am/sir is not our name, is it?

Notice that we have already entered our correct name and age. Run:

Epic! Now how did all this work? I will not explain this one to you. I want you to figure this thing out on your own. Once you’ve done that, share your insights and thoughts on the Comments section below and let’s see if you’ve really understand the flow of this code. Can’t wait to hear ya!

So that’s it! That’s all about functions with parameters and arguments in Python!

Next Lesson: Lesson 11: Escape Sequences in Python

3 Replies to “Lesson 10. Functions with Parameters and Arguments in Python”

Leave a Reply

Your email address will not be published. Required fields are marked *