How to Define a Function in Python: A Beginner’s Guide

Learn how to create reusable and modular code with Python functions. Our guide covers the basics of defining functions, including parameter passing and return values. Start improving your coding skills today!

Updated October 18, 2023

In Python, functions are blocks of code that perform a specific task. They are useful for organizing your code, reusing logic, and making your programs more modular and maintainable. In this article, we’ll go over the basics of defining functions in Python.

Basic Function Definition

To define a function in Python, you use the def keyword followed by the name of the function, a list of parameters in parentheses, and a colon :. Here’s an example of a simple function that takes two arguments, x and y, and returns their sum:

def add(x, y):
    return x + y

This function takes two arguments, x and y, and returns their sum. You can call this function by passing in the values for x and y, like this:

result = add(3, 5)
print(result)  # Output: 8

Function Parameters and Return Types

Functions can have parameters, which are used to pass data into the function. In the example above, we defined add with two parameters, x and y. The return type of a function is the type of value that the function returns after executing its code. In the case of add, the return type is int, which means the function returns an integer value.

Here’s another example of a function that takes no arguments, has no parameters, and returns a string:

def greet(name):
    return "Hello, {}!".format(name)

This function takes one argument, name, and returns a string with the greeting message. You can call this function by passing in a name, like this:

result = greet("Alice")
print(result)  # Output: Hello, Alice!

Functions with Multiple Return Statements

In Python, functions can have multiple return statements. This is useful when you want to return multiple values from a function. Here’s an example of a function that takes one argument and returns two values:

def calculate_area(shape):
    width = shape[0]
    height = shape[1]
    area = width * height
    perimeter = width + height + width + height
    return area, perimeter

This function takes one argument, shape, which is a list of two values representing the width and height of a shape. The function returns two values: the area of the shape, and the perimeter of the shape. You can call this function by passing in a shape, like this:

shape = [3, 4]
result1, result2 = calculate_area(shape)
print(result1)  # Output: 12.0
print(result2)  # Output: 14.0

Lambda Functions

Lambda functions are small anonymous functions that can be defined inline. Here’s an example of a lambda function that takes one argument and returns its square:

def square(x):
    return x ** 2

This lambda function takes one argument, x, and returns its square. You can call this function by passing in a value, like this:

result = square(5)
print(result)  # Output: 25

Closures

Closures are functions that have access to the variables of their surrounding scope. Here’s an example of a closure function that takes one argument and returns its double:

def double(x):
    return x * 2

This closure function takes one argument, x, and returns its double. The function has access to the x variable from the surrounding scope, which means it can use the x variable to calculate the double. You can call this function by passing in a value, like this:

result = double(5)
print(result)  # Output: 10

That’s it! These are the basics of defining functions in Python. Functions are a powerful tool for organizing your code and making your programs more modular and maintainable. With these basics under your belt, you’re ready to start building your own Python programs. Happy coding!

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!