Those learning to code learn about functions very early on in their journey. Not only do beginners use Python functions right from their first lesson, but they also learn to define their own Python functions early on.
It may be tempting–and in my experience, it’s a common pitfall early on–for a relative beginner to think they know most of what there is to know about functions once they understand the basics. It can feel that if you know how to pass data into a function using arguments and how to return data using the return
statement, that’s all there is to know.
Usually, once a learner understands that variables defined in a function are local, they feel they’re at the top of the Python functions world!
In this series of short articles, I’ll explore some “intermediate” topics relating to functions in Python. I’ve used quotation marks for “intermediate” since I believe that terms like “beginner”, “intermediate”, and “advanced” are very subjective and can mean different things to different people.
So, what I mean by “intermediate” is that these are topics that you wouldn’t come across at first when learning about Python functions (except for this first article in the series). However, these topics are good to know once you’ve mastered the basics.
Overview Of The Intermediate Python Functions Series
In this series of seven articles, all of which will be relatively brief, I’ll cover the following topics:
- [This article/Introduction to the series] Do you know all your functions terminology well?
- Choosing whether to use positional or keyword arguments when calling a function
- Using optional arguments by including default values when defining a function
- Using variable numbers of positional and keyword arguments:
*args
and**kwargs
- Using positional-only arguments and keyword-only arguments: the “rogue” forward slash / or asterisk * in function signatures
- Type hinting in functions
- Best practices when defining and using functions
This series of articles is for those who learnt the basics of how to define a function and are now ready to move on to the next level. If you want to learn the basics of defining functions, you can read the chapter Power-up Your Coding: Create Your Own Functions in The Python Coding Book.
What’s In A Name? Getting The Terminology Right
Here are four terms that we constantly use when talking about functions:
- Define
- Call
- Parameter
- Argument
You’re likely to be familiar with all of them already. I’ll review the first two to ensure we’re all on the same page. The third and fourth are often confused with each other. They do not refer to the same thing!
Let’s look at a simple example to make sure we know what all of these terms mean:
def greet_person(person): print(f"Hello {person}! How are you doing today?") greet_person("Ishaan") greet_person("Elizabeth")
Define
You define a function when you use the def
keyword to create a new function. In the example shown above, the function definition includes the first two lines.
The first line of the function definition is the function signature. The indented block of code after the colon is the code that will be executed when you use the function.
The function definition alone–the first two lines above–is not enough to execute the code in the definition. For this, we need to call the function…
Call
You call the function when you use it. You call the function when writing the name of the function followed by parentheses –the round brackets ()
.
When you call a function, the code that follows the colon in the definition is executed. In the example I showed earlier, there are two function calls:
greet_person("Ishaan") greet_person("Elizabeth")
This means the function is executed twice.
What about the stuff in the brackets? Let’s move on to parameters and arguments…
Parameter
A parameter is the name you choose to use within a function definition for any information that’s needed for the function to run. You add parameters in the brackets in the function signature.
In the example I’m using here, the parameter is person
. This function only has one parameter, but you can have more. You’ll see more examples with many parameters later in the series.
You can think of the parameter as the name of a “storage box” which is ready to hold any information you send into the function when you call it.
However, this “box” is empty when you define the function. It has a label with a name but no contents. So you can create a mental image of an empty box with the label person
stuck to it.
Argument
An argument is the actual information you send to a function when you call it. In the above example, you called the function greet_person()
twice:
- In the first call, the argument is
"Ishaan"
- In the second call, the argument is
"Elizabeth"
When you call the function, this information (the argument) is stored in a variable called person
inside the function.
In summary, the parameter is the name you choose when you define a function. This name will be used to store any future information you use when you call the function. The argument is the data you send when you do call the function. The parameter is the label of the “storage box”, while the argument is its contents.
Do The Names Matter?
Don’t worry too much if you confuse the terms parameter and argument. Many programmers confuse these terms, too. And this leads nicely to a common question: “Does it matter if I don’t know what something is called as long as I know how to use it?”
If you only need to learn one of the two things, then knowing how to use a tool is better than knowing what it’s called! However, as you use functions more and more, it’s helpful to start using the terms in the correct context. It will also help you when reading documentation since most documentation will use the terms in their specific meanings. Therefore, you’ll be better placed to understand what a function does and how to use it a lot better and quicker if you know what the terms mean.
In the next article in this Intermediate Python Functions Series, I’ll talk about how, in many cases, you can choose to use arguments either as positional arguments or as keyword arguments when calling a function. These terms will often recur in these articles as we talk about more advanced topics later in the series.
Next Article:<Link will be posted here when the next article in the series is posted>
Further Reading
- Chapter 3: Power-up Your Coding: Create Your Own Functions for an in-depth introduction to Python functions
- Chapter 6: Functions Revisited. This chapter covers topics that will be dealt with later on in the series
- The White Room: Understanding Programming. In this article, I briefly referred to parameters as boxes which store data. This is part of a broader analogy I like to use. You can read more in this chapter
- Using Python Optional Arguments When Defining Functions is an article I wrote for Real Python if you want to read ahead. This is a topic I’ll deal with again later on in this series
Get the latest blog updates
No spam promise. You’ll get an email when a new blog post is published
The post Moving On From The Basics of Python Functions [#1 in Intermediate Python Functions Series] appeared first on The Python Coding Book.