Functions are the building blocks of Python programs. They let you write reusable code, reduce duplication, and make projects easier to maintain. In this guide, we’ll walk through all the ways you can call functions in Python, from the basics to advanced techniques—complete with examples and best practices.

👉 Before you dive in, make sure you’re comfortable with Python variables and data types.

Method 1 — Direct Function Call

The simplest way to call a function is by using parentheses () after the function’s name.

def greet():
    print("Hello, World!")

greet()

Method 2 — Call with Arguments

Functions often take arguments. Python supports positional, keyword, default, variable-length (*args, **kwargs), positional-only (/), and keyword-only (*) arguments.

def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("Alice")  
greet("Bob", "Hi")  

👉 You’ll often pass lists or collections into functions and process them using loops in Python.
👉 When passing text, check out our Python string methods guide.

Method 3 — Functions That Return Values

Functions can return values using return.

def add(a, b):
    return a + b

result = add(2, 3)
print(result)

👉 Always validate return values and use try/except for error handling.

Method 4 — Call Functions by Name (Dynamic Calls)

Sometimes you may want to call a function when its name is stored in a string. Instead of unsafe eval(), use getattr() or importlib.

import math
func_name = "sqrt"
result = getattr(math, func_name)(16)
print(result)  # 4.0

👉 Check out our guide on eval() vs exec() to see why direct evaluation can be risky.

READ 👉  How to Extract Subtitles from YouTube Videos Using Python (With Code)

Method 5 — Use a Dispatch Dictionary

You can create a dictionary mapping strings to functions for clean, dynamic calls.

def add(a, b): return a + b
def sub(a, b): return a - b

dispatch = {"+": add, "-": sub}
print(dispatch["+"](3, 4))  # 7

Method 6 — Call Functions from Another File

Functions can be organized into modules and imported across files.

# In utils.py
def greet(name):
    print(f"Hello, {name}!")

# In main.py
from utils import greet
greet("Alice")

👉 To learn more about structuring projects, see this guide on Python modules and packages.

Method 7 — Real-World Examples

Here’s how functions are called in popular Python libraries:

import numpy as np
arr = np.array([1, 2, 3])  # NumPy function

from flask import Flask
app = Flask(__name__)      # Flask function

import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3]})  # Pandas function

👉 Learn more with this guides on NumPy, Flask, and Pandas DataFrames.

Common Pitfalls When Calling Functions

  • Forgetting parentheses: greet vs greet()
  • Passing the wrong number of arguments
  • Calling a function before it’s defined
  • Confusing return vs printing

Quick Reference Cheatsheet

TechniqueExample
Direct callgreet()
With argsgreet("Alice")
Return valuesresult = add(2, 3)
By namegetattr(math, "sqrt")(16)
Dispatch dictionarydispatch["+"](3, 4)
From another filefrom utils import greet

Conclusion

Calling functions in Python is one of the most fundamental skills for writing clean, reusable, and powerful code. From simple function calls to advanced patterns like dispatch dictionaries and imports across modules, mastering these techniques will make you a more effective Python programmer.

Did you enjoy this article? Feel free to share it on social media and subscribe to our newsletter so you never miss a post!

And if you'd like to go a step further in supporting us, you can treat us to a virtual coffee ☕️. Thank you for your support ❤️!
Buy Me a Coffee

Categorized in: