Python Function

A Python function is a block of code that only runs when you call it. You can optionally send data (arguments ) to the function and the function can optionally return data.

Functions help to reduce redundant code, which makes your code cleaner and easier to read.

We used functions before. For example, the print function we used at the beginning of this course where “print” is the function name and the argument is the value you want to print. Python has many built-in functions.

In this lesson, I’ll explain how functions work, how we create our own functions, and I’ll give you an example of how functions help to improve our code.




Create and Call Function

Let’s create our first function. You create a function by starting with “def”, giving it a name, and ending with parentheses. To call the function, we specify the function name and end with parentheses. Here is an example:

Above, we have a function named “show_connection_message”. The only thing it does is print a message.

Arguments

We can also send information to a function by using arguments. This can be anything: A string, integer, float, list, tuple, dictionary, etc.

When you create the function, you specify the arguments after the function name, inside parentheses. Python uses two different terms for the information we send to a function, based on from which perspective you look at it:

  • Argument: From outside the function’s perspective, this is the data we send to the function.
  • Parameter: From inside the function’s perspective, this is the variable in the function which receives data from the argument.

Let me show you an example:

In the code above, the string “SW1” is the argument we send to the function. From within the function, the parameter is the variable “device_name” which receives the string “SW1”.

Multiple Arguments

A function can have multiple arguments. To add extra arguments, use a comma in between. Here is an example:

Return Data

In the examples above, we sent data into the function. A function can also return data to us. We do this by ending the function with “return” and supplying the information we want to return. Here is an example:

The function above requires an integer (200 or 404) and returns a string.

Example

You now know the syntax to create and call a function, but you might be wondering where and when to use them. Let’s walk through an example.

Suppose we have a script that connects to a network device. Every time our script connects, we want to show messages to the user that show the status of the connection. Here is our code:

This code works, but it doesn’t look very efficient. We use the exact same print messages to connect to SW1 and SW2.

When writing code, you shouldn’t repeat yourself. To reduce the amount of reused code, we can use a function. Let me show you an example:

Above, we have a function that prints two messages. We run the function twice, once for each device. The output is the exact same as before. Less code, same result. That’s what we are looking for.

We can optimize our code even more though. Take a look at these two lines:

print("Attempt to connect to device %s in progress." % first_device)
print("Attempt to connect to device %s in progress." % second_device)

The two lines above are exactly the same, except the variable is different. Let’s modify our code so that the function uses an argument:

Our function now includes an argument named “network_device_name”. It also includes our print message which shows the “Attempt to connect to device in progress” string with the “network_device_name” variable.

We call our function two times, including a variable we want to use as the function’s argument. Our output is the exact same. By using a function, we removed some redundant code in our script.

If you have many devices you want to connect to, you could use a list and a for loop. Here is an example:

The above uses our function in both of the for loops, which reduces the number of print lines we need.

Conclusion

You have learned how to use Python functions:

  • A function is a block of code that only runs when you call the function.
  • Functions can accept data with arguments.
  • Functions can return data.
  • Functions help to reduce redundant code and make your code easier to read.

I hope you enjoyed this lesson. If you have any questions feel free to leave a comment!


Forum Replies

  1. Hello Diego

    Yes, for Python 3, and later back-ported to Python 2.7, it is possible to do a more “regular” formatting of strings rather than using the %s format. This is considered the “new style” compared to the use of the % operator.

    Thanks for sharing!

    Laz

1 more reply! Ask a question or join the discussion by visiting our Community Forum