Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to call a function in Python

Introduction

In this blog post, we will be focusing on one of the most fundamental concepts in programming: functions. Specifically, we will be exploring how to call a function in Python. If you're new to programming, don't worry! We will be breaking down everything in a simple and easy-to-understand manner.

A function can be thought of as a small piece of code that performs a specific task. Imagine, for example, that you have a robot that can perform a variety of tasks, such as making coffee, folding laundry, or cooking dinner. Each of these tasks can be thought of as a function, and the robot performs them by following a set of instructions.

In programming, functions help us break down complex tasks into smaller, more manageable pieces. They also allow us to reuse code, which makes our programs more efficient and easier to maintain. So, let's dive in and learn how to call a function in Python!

What is a function?

Before we learn how to call a function, it's important to understand what a function is and why we use them. In Python, a function is a block of code that is organized and reusable. Functions are used for two main reasons:

  1. Code reusability: Functions allow you to write code once and reuse it multiple times. This helps to make your code more efficient, as you don't need to repeat the same code in different parts of your program.
  2. Modularity: Functions help you break down complex tasks into smaller, more manageable pieces. This makes your code more organized and easier to understand.

In Python, you can create your own functions or use built-in functions that are provided by the language. Let's start by looking at how to call built-in functions.

Calling built-in functions

Python comes with many built-in functions that you can use in your programs. These functions are part of the Python standard library and are available to you without needing to import any additional modules.

Some common built-in functions include:

  • print(): displays the specified message on the screen
  • len(): returns the number of items in a list, tuple, or string
  • sum(): returns the sum of all items in a list or tuple

To call a built-in function, you simply write the name of the function followed by parentheses () and any required arguments inside the parentheses. Here's an example of how to call the print() function:

print("Hello, World!")

When you run this code, Python will call the print() function and display the message "Hello, World!" on your screen.

Now let's see how to call the len() function. Suppose we have a list of numbers and we want to find out how many items are in the list. We can do this using the len() function:

numbers = [1, 2, 3, 4, 5]
length = len(numbers)
print("There are", length, "items in the list.")

In this example, we first create a list called numbers. We then call the len() function with numbers as an argument, which returns the number of items in the list. Finally, we use the print() function to display the result.

Defining your own functions

In addition to using built-in functions, you can also create your own functions in Python. To define a function, you use the def keyword followed by the name of the function and a pair of parentheses (). Inside the parentheses, you can specify any parameters that your function requires.

Here's an example of a simple function that takes two numbers as input and returns their sum:

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

In this example, we define a function called add_numbers with two parameters, a and b. The function calculates the sum of the two numbers and returns the result.

To use this function, you need to call it with the required arguments. Here's how to call the add_numbers function:

sum = add_numbers(3, 4)
print("The sum is", sum)

When you run this code, Python will call the add_numbers function with the arguments 3 and 4. The function will then calculate the sum of the numbers and return the result, which we store in the variable sum. Finally, we use the print() function to display the result.

Function arguments

In the previous examples, we saw that functions can take input in the form of arguments. When calling a function, you need to provide the values for these arguments.

There are two types of arguments in Python:

  1. Positional arguments: These are the most common type of arguments. They are passed to the function in the same order as they are defined in the function declaration. In the add_numbers function example, a and b are positional arguments.
  2. Keyword arguments: These are arguments that are passed to the function using the name of the parameter followed by an equal sign = and the value. Keyword arguments can be provided in any order when calling the function.

Here's an example of how to use keyword arguments when calling a function:

def greet_person(first_name, last_name):
    print("Hello,", first_name, last_name)

greet_person(first_name="John", last_name="Doe")

In this example, we define a function called greet_person that takes two parameters, first_name and last_name. When calling the function, we provide the values for these parameters using keyword arguments.

Default parameter values

Sometimes, you may want to provide a default value for a function parameter. This can be useful if you want to make certain arguments optional. To specify a default value for a parameter, you can use the equal sign = followed by the default value in the function declaration.

Here's an example of a function with a default parameter value:

def repeat_message(message, count=1):
    for i in range(count):
        print(message)

repeat_message("Hello")

In this example, we define a function called repeat_message that takes two parameters: message and count. We provide a default value of 1 for the count parameter.

When calling the repeat_message function, we only provide a value for the message parameter. Since we don't provide a value for the count parameter, Python will use its default value of 1.

Conclusion

In this blog post, we covered the basics of calling functions in Python. We learned about built-in functions, how to define your own functions, and how to work with function arguments and default parameter values.

Functions are an essential concept in programming, as they allow you to break down complex tasks into smaller, more manageable pieces and reuse code to make your programs more efficient and easier to maintain. Now that you have a solid understanding of how to call functions in Python, you can start applying these concepts to your own projects and take your programming skills to the next level!