To call a function in Python, add parenthesis after the function name.
For example, if you have a function called greet
, you can call it by:
greet()
And if the function takes arguments, specify them inside the parenthesis:
greet("Nick")
Let’s take a deeper dive into functions and how to call them in Python. More specifically, we are going to take a look at how to call functions:
- With no arguments.
- With arguments.
- With keyword arguments.
- With any number of arguments.
- With any number of keyword arguments.
- From another file.
1. How to call a function without arguments in Python
This is a basic example of how to call a function in Python.
A function that takes no arguments can be called by adding parenthesis after the name of the function.
For example, let’s create a function greet
.
def greet(): print("Hello world!")
And now you can call the function by:
greet()
This results in the following output to the console:
Hello world!
2. How to call a function with arguments in Python
It is common for a function to accept an argument or arguments to work with.
To call a function that takes arguments, specify the arguments inside the parenthesis when calling the function.
For example, let’s create a function that accepts one argument:
def greet(name): print("Hello,", name)
And let’s call this function:
greet("Nick")
This results in the following output to the console:
Hello, Nick
Let’s also demonstrate functions that take more than one argument:
def greet(firstname, lastname): print("Hello,", firstname, lastname)
Now you can call the function by:
greet("Nick", "Jones")
This produces the following output:
Hello, Nick Jones
3. How to call a function with keyword arguments in Python
Python functions can accept two types of arguments:
- Positional arguments
- Keyword arguments
When it comes to positional arguments, order matters.
For example, in the previous example, the function took two positional arguments:
def greet(firstname, lastname): print("Hello,", firstname, lastname)
The function expects these arguments to be in the right order. The first argument should be the first name and the last argument should be the last name.
However, if you pass the arguments as keyword arguments, you may swap their order freely.
A keyword argument is an argument where you name the argument you pass into a function.
Let’s call the greet
function from the previous example by providing it with the arguments as keyword arguments:
greet(lastname="Jones", firstname="Nick")
Output:
Hello, Nick Jones
As you can see, using keywords allowed you to swap the order of the arguments.
4. How to call a function with any number of arguments in Python
Sometimes you don’t know how many arguments you want to pass into a function. Thus, it is possible to call a function with an arbitrary number of arguments.
To demonstrate, let’s create a function that accepts any number of arguments. To make this possible, the function argument needs to be preceded by an asterisk *
:
def greet(*args): for name in args: print("Hello,", name)
Now you can call the function with any number of arguments
greet("Nick") greet("Matt", "Sophie", "Jon")
Output
Hello, Nick Hello, Matt Hello, Sophie Hello, Jon
You can also pass the arguments as an array. But if you do this, add the asterisk before the array to unpack the arguments for the function:
greet(*["Matt", "Sophie", "Jon"])
Hello, Matt Hello, Sophie Hello, Jon
5. How to call a function with any number of keyword arguments in Python
You may not know how many keyword arguments to pass into a function. But this is no problem. Python lets you design functions in such a way that you can pass any number of keyword arguments into it.
To demonstrate, let’s create a function that accepts any number of keyword arguments. To make this possible, the argument needs to be preceded by a double asterisk **
:
def greet(**kwargs): for literal, name in kwargs.items(): print("Hello,", name)
Now you can call the function with any number of keyword arguments, and name the arguments however you like:
greet(name="Marie") greet(name="Jane", otherName="Ann")
Output:
Hello, Marie Hello, Jane Hello, Ann
You can also call this function by passing a dictionary as an argument. If you do this, remember to unpack the dictionary with the double-asterisk **:
greet(**{"name": "Jane", "otherName": "Ann"})
Output:
Hello, Jane Hello, Ann
6. How to call a function from another file in Python
To call a function from another Python file, you need to import the file and call the functions from it.
Here is an illustration where a function is called from another file:
If you want to import a specific function from another file, you can specify the name of the function in the import statement as seen above.
But if you want to import all the functions from another file, use *
in the import statement.
For example:
from somefile import *
Conclusion
Today you learned how to call a function in Python.
To recap, functions allow for grouping useful code and logic into a reusable block of code. When you call a function in Python, you’re running the code inside the function.
In Python, you can call functions with:
- No arguments.
- Arguments.
- Keyword arguments.
- Any number of arguments.
- Any number of keyword arguments.
- Another file.
Thanks for reading. I hope you enjoy it.
Happy coding.