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!
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))

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))

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))

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")

We can also overwrite the default values:
print_weather("Mike", "Boston", "28 degrees Celsius")

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.
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
What is a function in Python?
A function in Python is a reusable block of code designed to takes input(s), perform a specific operation and return an output.
How do you define a function in Python?
To define a function in Python, use the def
keyword, followed by the function name and parentheses containing any parameters.
What does *args and **kwargs do in a Python function?
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.