Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a function in Python

Introduction

Functions play an essential role in any programming language, and Python is no exception. As someone who's learning programming, understanding the concept of functions and how to create them can help you become a better programmer. In this post, we will cover the basics of functions in Python, explain them in simple terms, and provide code examples to help you understand them better. So let's dive right in!

What is a function?

A function in programming is a reusable block of code that performs a specific task. Think of it as a machine that takes some input, processes it, and then produces an output. Functions help you organize your code, reduce repetition, and make your code more readable and easier to maintain.

For example, imagine you want to calculate the square of a number. Instead of writing the same code multiple times for different numbers, you can create a function that takes the number as input, calculates the square, and returns the result. This way, you can use the same function whenever you need to calculate the square of a number.

Defining a function in Python

In Python, you can define a function using the def keyword, followed by the function name and a pair of parentheses. The code block inside the function is indented, which tells Python that the code belongs to the function. Here's a simple example of a function that takes a number as input and returns the square of the number:

def square(number):
    result = number * number
    return result

Let's break down this example: - def: This keyword tells Python that you're defining a function. - square: This is the name of the function. It should be a meaningful name that describes what the function does. - (number): These parentheses contain the input (also called "arguments" or "parameters") that the function accepts. In this case, the function takes a single input called number. - result = number * number: This line of code calculates the square of the input number and stores the result in a variable called result. - return result: This line tells the function to return the value stored in the result variable. The return keyword is used to specify the output of the function.

Calling a function

Once you've defined a function, you can "call" or "invoke" it by using its name followed by a pair of parentheses containing the input values. Here's how you can call the square function we defined earlier:

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

In this example, we called the square function with the input value 5. The function calculated the square of 5 and returned the result, which we stored in a variable called square_of_five. We then printed the value of square_of_five, which is 25.

Multiple input parameters

A function can accept multiple input parameters. To do this, you simply separate the parameters with commas inside the parentheses when defining the function. Here's an example of a function that calculates the sum of two numbers:

def add_numbers(a, b):
    result = a + b
    return result

In this example, the function add_numbers takes two input parameters: a and b. The function calculates the sum of these two numbers and returns the result.

You can call this function by providing two input values, like this:

sum_of_numbers = add_numbers(3, 4)
print(sum_of_numbers)  # Output: 7

Default parameter values

Sometimes, you might want to provide default values for some input parameters in your function. This means that if the user doesn't provide a value for that parameter, the default value will be used instead. You can set default parameter values by using the assignment operator (=) when defining the function.

Here's an example of a function that calculates the power of a number with a default exponent value of 2:

def power(number, exponent=2):
    result = number ** exponent
    return result

In this example, the function power takes two input parameters: number and exponent. The exponent parameter has a default value of 2. If the user doesn't provide a value for the exponent, the function will use the default value.

You can call this function with or without providing a value for the exponent parameter:

power_of_two = power(2)    # Uses the default exponent value (2)
print(power_of_two)        # Output: 4

power_of_three = power(2, 3)  # Provides a value for the exponent parameter (3)
print(power_of_three)         # Output: 8

Returning multiple values

In some cases, you might want a function to return multiple values. You can do this by returning the values as a tuple. A tuple is a collection of ordered, immutable elements, enclosed in round brackets. Here's an example of a function that calculates the area and perimeter of a rectangle:

def rectangle_properties(length, width):
    area = length * width
    perimeter = 2 * (length + width)
    return area, perimeter  # Returns the values as a tuple

In this example, the function rectangle_properties takes two input parameters: length and width. The function calculates the area and perimeter of the rectangle and returns the values as a tuple.

You can call this function and unpack the returned tuple into multiple variables:

rectangle_area, rectangle_perimeter = rectangle_properties(5, 4)
print("Area:", rectangle_area)         # Output: Area: 20
print("Perimeter:", rectangle_perimeter)  # Output: Perimeter: 18

Conclusion

In this post, we covered the basics of functions in Python, including how to define, call, and work with functions that have multiple input parameters, default parameter values, and multiple return values. We also provided code examples to help you understand the concepts better.

As you continue learning programming, you'll find that functions are an essential tool for organizing your code, reducing repetition, and making your code more readable and maintainable. By mastering the concept of functions, you'll be well on your way to becoming a better programmer. Happy coding!