How to Call a Function in Python?

In this tutorial, I will explain how to call a function in Python. As a Python programmer, while using functions as a part of the project, it is important to learn how to call a function. Let us learn more about calling functions in Python with examples and screenshots of executed example code.

Python Functions

Functions are a fundamental aspect of programming in Python. They allow you to encapsulate code into reusable blocks, making your programs more modular and easier to manage. A function in Python is defined using the def keyword, followed by the function name and parentheses. Here’s a simple example:

def greet():
    print("Hello, welcome to Python programming!")

In this example, greet is a function that prints a welcome message. But how do we call this function? Let’s explore the process in detail.

Read How to Get the Name of a Function in Python?

Call a Function in Python

To call a function, you simply use the function’s name followed by parentheses. If the function requires arguments, you pass them within the parentheses. Here’s how you can call the greet the function we defined earlier:

greet()

This will output:

Hello, welcome to Python programming!

You can see the output in the screenshot below.

Call a Function in Python

Check out How to Use the Input() Function in Python?

Example with Arguments

Let’s make this more practical by defining a function that takes arguments. Suppose we want to create a function that greets a user by name:

def greet_user(name):
    print(f"Hello, {name}! Welcome to Python programming.")

To call this function, you pass a string argument representing the user’s name:

greet_user("John")

This will output:

Hello, John! Welcome to Python programming.

You can see the output in the screenshot below.

How to Call a Function in Python

Read How to Use Default Function Arguments in Python?

Example

Let’s get into some practical examples using USA-specific names and scenarios. Imagine we are developing a program for a local library system in New York. We need functions to handle different tasks, such as registering a new member and checking out books.

1. Register a New Member

First, let’s create a function to register a new member. We’ll use the member’s first and last names as arguments:

def register_member(first_name, last_name):
    print(f"Member {first_name} {last_name} has been successfully registered.")

To call this function, you provide the first and last names of the new member:

register_member("Emily", "Johnson")

This will output:

Member Emily Johnson has been successfully registered.

You can see the output in the screenshot below.

Call a Function in Python Register a New Member

Read How to Exit a Function in Python?

2. Check Out Books

Next, let’s create a function to handle book checkouts. We’ll use the member’s name and the book title as arguments:

def checkout_book(member_name, book_title):
    print(f"{member_name} has checked out the book titled '{book_title}'.")

To call this function, you provide the member’s name and the title of the book they are checking out:

checkout_book("Michael Smith", "The Great Gatsby")

This will output:

Michael Smith has checked out the book titled 'The Great Gatsby'.

You can see the output in the screenshot below.

Call a Function in Python Check Out Books

Check out How to Call a Function Within a Function in Python?

Advanced Function Calling Techniques

Let us learn some important advanced function calling techniques.

1. Default Arguments

Sometimes, you may want to provide default values for function arguments. This allows the function to be called with fewer arguments. Here’s an example:

def greet_city(name, city="New York"):
    print(f"Hello, {name} from {city}!")

You can call this function with or without the city argument:

greet_city("Alice")
greet_city("Bob", "Los Angeles")

The output will be:

Hello, Alice from New York!
Hello, Bob from Los Angeles!

Read How to Use Static Variables in Python Functions?

2. Keyword Arguments

Python also allows you to call functions using keyword arguments, which can make your code more readable. Here’s an example:

def book_details(title, author, year):
    print(f"'{title}' by {author}, published in {year}.")

You can call this function using keyword arguments:

book_details(title="1984", author="George Orwell", year=1949)

This will output:

'1984' by George Orwell, published in 1949.

Check out How to Use Python Functions with Optional Arguments?

3. Arbitrary Arguments

If you need a function to accept an arbitrary number of arguments, you can use the *args syntax. Here’s an example:

def list_books(*books):
    print("Books in the library:")
    for book in books:
        print(f"- {book}")

You can call this function with any number of book titles:

list_books("1984", "To Kill a Mockingbird", "The Catcher in the Rye")

This will output:

Books in the library:
- 1984
- To Kill a Mockingbird
- The Catcher in the Rye

Read How to Use Lambda Functions in Python?

4. Arbitrary Keyword Arguments

To accept an arbitrary number of keyword arguments, use the **kwargs syntax. Here’s an example:

def member_info(**info):
    print("Member Information:")
    for key, value in info.items():
        print(f"{key}: {value}")

You can call this function with any number of keyword arguments:

member_info(name="Sarah Connor", age=35, city="San Francisco")

This will output:

Member Information:
name: Sarah Connor
age: 35
city: San Francisco

Read How to Use the Python Main Function with Arguments?

Conclusion

I have explained how to call a function in Python. I discussed the calling function with examples , and practical examples on registering a new member, checking out books. I also covered some important advanced function calling techniques.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.