Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to call function in Python

Understanding Functions in Python

Before we dive into the mechanics of calling a function in Python, let’s first understand what a function is. Think of it like a little machine within your code. You put something in, it carries out a specific task with whatever you put in, and then it gives something back out.

In Python, a function is defined using the def keyword, followed by a name of your choosing, and a pair of parentheses (). These parentheses can be left empty, or they can contain variables – these are the things that your 'machine' is going to work with. Finally, your function ends with a colon :.

Here's a simple example:

def greet():
    print("Hello, World!")

In this example, greet is the name of the function, and it's a machine whose task is to print the phrase "Hello, World!". Notice how it doesn't need any inputs to do its job – the parentheses are empty.

Calling a Function

To call a function means to activate that little machine and get it to do its task. You call a function simply by writing the name of the function, followed by a pair of parentheses. If your function requires inputs, this is where you put them.

Using our greet function from earlier, this is how you'd call it:

greet()

If you run this code, the greet machine will activate, and you'll see "Hello, World!" printed on your screen.

Functions with Inputs

Let's take a look at a function that requires an input. This is where the real power of functions starts to shine.

def greet(name):
    print("Hello, " + name + "!")

This version of greet requires one input – we've called it name. When you call this function, you need to put a name inside the parentheses:

greet("Alice")

If you run this code, the greet machine will activate, but this time it uses the name you gave it. You'll see "Hello, Alice!" printed on your screen.

Functions with Multiple Inputs

Functions aren't limited to just one input. You can put as many as you need, separated by commas:

def greet(name, time_of_day):
    print("Good " + time_of_day + ", " + name + "!")

When you call this function, you need to provide both a name and a time of day:

greet("Alice", "morning")

If you run this code, the greet machine will activate, and you'll see "Good morning, Alice!" printed on your screen.

Functions that Return Values

So far, our little machines have been doing their tasks and then printing the result directly. But sometimes, you want your machine to give the result back to you without printing it. This is where the return keyword comes in.

def add_numbers(num1, num2):
    return num1 + num2

This add_numbers machine takes two inputs, adds them together, and then gives the result back. When you call this function, you can store the result in a variable:

sum = add_numbers(5, 3)
print(sum)

If you run this code, the add_numbers machine will activate, add the numbers together, and then give the result back. The result is stored in the variable sum, and then printed, so you'll see "8" on your screen.

Conclusion

Imagine functions as little machines, each designed to perform a specific task. These machines take in some input, perform their task with the input, and then give you something back. This way of thinking about functions can help demystify some of the jargon and technicalities, making Python functions much easier to understand and use.

The power of functions lies in their ability to perform tasks repeatedly and consistently. They are the building blocks that we use to construct larger and more complex programs. Understanding how to call functions, pass in the required input, and handle the output, is a crucial skill in Python programming.

So, keep practicing, keep coding, and remember - every complex program is just a collection of simple functions working together. Happy coding!