What Is a Function in Python?

A function in Python is a defined section of code that takes an input, performs a specific task and provides an output. Here, our expert introduces you to how they work in Python.

Written by Sadrach Pierre
A pencil lays on a piece of graph paper with a function on it
Image: Shutterstock / Built In
Brand Studio Logo
UPDATED BY
Brennan Whitfield | May 16, 2025
Summary: A Python function is a named block of code that takes input, performs a task and returns output. This article covers defining functions, using *args for variable positional arguments, and **kwargs for keyword arguments, with examples to help users experiment and learn.

A function in Python is a reusable block of code designed to perform a specific operation, typically based on inputs (called parameters) and returning an output.

Python treats functions as first-class objects, meaning they can be assigned to variables, passed to other functions and returned as values — enabling powerful patterns like decorators and higher-order functions.

Defining Functions in Python

A function in computer programming is a named section of a code that performs a specific task. This typically involves taking input code, manipulating it and returning an output from the function.

In this post, we will go over how to define Python functions, how to specify functions that can take an arbitrary number of positional arguments, and how to specify functions that can take an arbitrary number of keyword arguments.

Let’s get started!

RelatedHow to Define Empty Variables and Data Structures in Python

 

How To Use Functions In Python (Python Tutorial #3). | Video: CS Dojo

Examples of Functions in Python

To start, let’s define a function that takes two arguments (in this case, two numbers) and returns the sum.

Use the def keyword to define the function, like so:

def sum_function(first, second):
    return (first + second)

Let’s test our function with the numbers five and 10:

print("Sum: ", sum_function(5, 10))
A computer output
Image: Screenshot by the author.

This gives the expected result.

Using *args in Python Functions

We can use the star expression (*) in Python to pass in an arbitrary number of arguments. For example, we can define a new function:

def sum_arbitrary_arguments(first, *rest):
    return (first + sum(rest))

As a note, this function assumes the first value is required; others are optional.

Let’s call this function with the numbers five, 10 and 15:

print("Sum with arbitrary number of arguments: ", 
sum_arbitrary_arguments(5, 10,15))
A computer output
Image: Screenshot by the author.

We can call this function with any number of positional arguments. Let’s call the function with five, 10, 15, 20, 25, and 30:

print("Sum with arbitrary number of arguments: ", 
sum_arbitrary_arguments(5, 10,15, 20, 25, 30))
A computer output
Image: Screenshot by the author.

This gives the correct value for the sum. We can also specify a function with default keyword arguments. For example, consider a function that, by default, prints the weather in New York:

def print_weather(name, state = "New York", temp = "30 degrees 
Celsius"):
   print("Hi {}, it is {} in {}".format(name, temp, state))

If we call the function with “Sarah,” we get this:

print_weather("Sarah")
A computer output
Image: Screenshot by the author.

We can also overwrite the default values:

print_weather("Mike", "Boston", "28 degrees Celsius")
A computer output.
Image: Screenshot by the author.

Using **kwargs in Python Functions

We can also write a function that takes an arbitrary number of keyword arguments using the ** expression.

Let’s do this for weather reporting:

def print_weather_kwargs(**kwargs):
    weather_report = ""
    for argument in kwargs.values():
        weather_report += argument
    return weather_report

We then define a variable “kwargs,” for keyword arguments, as a dictionary and pass it into our function:

kwargs =  { 'temp':"30 degrees Celsius ", 'in':'in', 'State':" New 
York", 'when': ' today'}
print(print_weather_kwargs(**kwargs))

I’ll stop here, but I encourage you to play around with the code yourself.

RelatedBase 12: An Introduction

 

Experiment With Functions in Python

To summarize, in this post we discussed how to work with functions in Python. First, we defined a simple function that allows us to find the sum of two numbers, then we showed that we can use the star expression to define a function that can take an arbitrary number of positional arguments. Next, we showed how to define a function with keyword arguments. Finally, we showed how to define a function that can take an arbitrary number of keyword arguments.The code from this post is available on GitHub

Frequently Asked Questions

A function in Python is a reusable block of code designed to takes input(s), perform a specific operation and return an output. 

To define a function in Python, use the def keyword, followed by the function name and parentheses containing any parameters.

In Python, the *args syntax lets you pass a variable number of positional arguments to a function. The **kwargs syntax allows a function to accept a variable number of keyword arguments as a dictionary.

Explore Job Matches.