In this tutorial, you will learn what functions are and why they are so important. In computer programming, a function is a procedure (or a set of instrunctions) that can be invoked multiple times. Functions are fundamental to every programming language. In particular, Python classes (an advanced topic) are built upon the concept of functions.
Function Syntax
How do we define a function in Python? Let’s consider a basic example: we want to create a function that prints the message “Hello, world!” when invoked.
def say_hello(): # define our function
print("Hello, world!") # set of instructions, only the print in this case
We define a function using the following syntax: ‘def function_name():‘ At line 2 we specify the set of instructions that the function will perform. However, if you run the code above, you’ll easily notice that nothing happens, even though no error occurs. That’s because we have not called the function to action yet.
# ... copy the function above say_hello() # call our function to action # Output: # 'Hello, world!'
At line 3, we expressively call the function to action using the following syntax: function_name(). In this way, the code defined within the function is performed.
Function Parameters
It is possible to pass data into a function. These data are known as parameters and the function can perform operations with them. This makes functions very dynamic. Consider a case where you want to perform a simple addition between two numbers using a function. Presumably, you would want your function to work with any pair of numbers. Let’see how this works in code:
def addition(x, y): # x e y are parameters
result = x + y # perform the addition
print(result)
addition(2, 3) # invoke the function and pass 2 and 3 as parameters
# Output: 5
addition(7, 2) # invoke the function and pass 7 and 2 as parameters
# Output: 9
addition(2, 9) # invoke the function and pass 2 and 9 as parameters
# Output: 11
In the code above, x and y serve as parameters for the addition function. When defining our function, x and y are generic terms, making the function capable of working with any inputs (or parameters). For instance, x and y make sense when we invoke the function at line 5. In this case, x becomes the value 2 and y becomes the value 3 for the addition function.
As you can see, when invoking our addition function, we pass exactly two numbers. But what happens if we don’t pass any parameters to the function? Or if we pass more than two?
# ... copy the addition function from the above example addition() # invoke function without parameters # TypeError: addition() missing 2 required positional arguments: 'x' and 'y' addition(2, 5, 7) # try performing addition function with three numbers # TypeError: addition() takes 2 positional arguments but 3 were given
In both cases, an error occurs. As always, the error message displayed by Python is quite intuitive. When invoking the addition function with fewer than 2 parameters, Python informs us that the function is missing required arguments. On the other hand, when invoking the addition function with more than two parameters (e.g., three parameters like in this case), Python informs us that we are passing too many arguments.
Exercise
Let’s put what we’ve learned so far about functions into practice.
Define a function that accepts a string as input and calculates its length. Then, if the length is less than 10, the function prints the message “The string has less than 10 characters“, otherwise it prints “The string counts 10 characters or more“.
def my_function(input_string):
length = len(input_string)
if length < 10:
print("The string has less than 10 characters.")
else:
print("The string has 10 characters or more.")
In the next tutorial, we’ll build a more complex program based on functions, to reinforce the concepts that you’ve learned so far.