# Functions in Python—easy or hard to learn & master?

This week I'll be looking at aspects of functions which can be tricky for those who know basics and want to move to next level. If you've learnt about functions in Python are keen to take the next step, then hopefully this series of toots will help…

Let's start with the unexciting but important—terms

• _define_
• _call_
• _parameter_
• _argument_

Here's the code we'll use as an example which you can also get from the ALT text of the image in this toot:

`def greet_person(person):`
`print(f"Hello {person}! How are you doing today?")`

`greet_person("Ishaan")`
`greet_person("Elizabeth")`

You can follow this thread in the replies (unlisted)

/1

# — Define —

You _define_ the function when you use the `def` keyword

The first line of the definition is the function signature and the code after the colon is the code you want the function to perform…

However, this code does not run when you define a function

For this, we need to call the function…

/2

# — Call —

You _call_ a function when you use it

You're calling a function when you write its name followed by parenthesis (the round brackets)…

The code in the function definition will run when you call a function

In the example above, you're calling the function twice on the last two lines of the code

/3

# — Parameter —

A _parameter_ is the name you choose for information that's needed by the function

You add parameters inside the brackets in the function signature which is the line which includes `def`

In this example, the parameter is `person`

This is the name of the "storage box" which is ready to hold any information you send into the function

However, when you define the function, this "box" is still empty…

/4

# — Argument —

An _argument_ is the actual information you send to a function when you call it

You called the function twice in the example above

The first time you called `greet_person()` you used the argument `"Ishaan"` and the second time `"Elizabeth"`

When you call the function, the information (the argument) is stored in a variable named `person` inside the function

Don't worry too much if you confuse parameters and arguments. Many programmers confuse them, too!

/5

"What's in a name?", one might argue? And there's a point. What matters most is knowing how to use concepts rather than knowing the precise definitions

Still, these terms are used everywhere-in documentation, in tutorials, when talking to other programmers

So, knowing the terminology _is_ important. It shouldn't be the first thing someone learns, but eventually, everyone should become familiar with the right terms for the right things

/6

Later this week, I'll look at other aspects relating to that can confuse those beginners moving on to more intermediate levels, including:

• _positional and named arguments_
• _optional arguments_ (by defining default values)
• the "obscure" _args and kwargs_
• forcing _positional-only_ or _keyword-only_ arguments using the "rogue" forward slash `/` and asterisk `*` in the function signature
• _type-hinting_ when defining functions
• some best practices when defining and using functions

Some of these topics I've written about a while ago in this Real Python article: realpython.com/python-optional

@s_gruppetta I support the freedom of source code to call parameters arguments. 😁 docs.python.org/3/library/subp But I'm not looking for an parameter with you.😉

@folkerschamel Some may say it's deliberate flexibility, but I think the trend is to use each one more consistently. But yes, there are many exceptions out there, including in the docs!

@folkerschamel redefining `print()` could be one way. Redefining `__add__()` another, there are probably more…

Thanks @s_gruppetta! I use Python a lot and I like reading bits of knowledge like this as a way to learn and re-learn parts of the language and coding practices!

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.