HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

4) Functions and Scope Lesson

How to Call a Function in Python

4 min to complete · By Martin Breuss

You've successfully defined a new function, greet():

def greet(greeting, name):
    sentence = f"{greeting}, {name}! How are you?"
    return sentence

Now, you want to use the code you wrote to accomplish something. In this lesson, you'll learn how to execute your functions.

Function Calls

Functions help you to reduce repeating code, which makes it easier to write and maintain your code over the long term. Once you've defined your function, you can now use it as often as you want to:

def greet(greeting, name):
    sentence = f"{greeting}, {name}! How are you?"
    return sentence

print(greet("Hello", "World"))  # Hello, World! How are you?
print(greet("Howdy", "partner"))  # Howdy, partner! How are you?
print(greet("Zdravo", "prijatelj"))  # Zdravo, prijatelj! How are you?
print(greet("Hi", "Martin"))  # Hi, Martin! How are you?

To execute the code inside your function, you need to call the function. You've done this before, so it might not look all that new to you.

To call a function, you write its name followed by opening and closing parentheses. Optionally, if the function requires arguments, you need to pass the right amount of arguments in between the parentheses:

def my_func():  # Function without parameters
    pass

def my_param_func(param):  # Function with parameter
    pass

my_func()  # Function call without required arguments
my_param_func(42)  # Function call with the required argument

What happens when you call a function without passing the right amount of arguments?

Passing Arguments to Functions

  • What happens when you call greet() without passing any arguments to it?
  • What if you pass only one argument?
  • What happens if you pass numbers instead of strings to greet()?

Whenever you call a function without passing it the required amount of arguments, Python tells you about it with an error:

def greet(greeting, name):
    sentence = f"{greeting}, {name}! How are you?"
    return sentence

# Calling greet without passing any arguments
greet()  

# results in an error
# TypeError: greet() missing 2 required positional 
# arguments: 'greeting' and 'name'

Keep in mind that with your function definition, you're creating a blueprint for the function calls. You'll then need to stick to this blueprint and call your function as it was defined.

However, there are situations when the number of arguments you want to pass to a function might change in different use cases. Python has a solution for that through a language feature called *args and **kwargs. You'll learn more about it in the upcoming lesson.

Summary: How to Call a Function in Python

  • To execute a function, you need to call it. You do that by writing the function's name followed by opening and closing parentheses.
  • If your function requires parameters, you need to pass the right amount of arguments in between the parentheses.