USING FUNCTIONS & MODULES

The other concepts on functions (INTRO TO FUNCTIONS, FUNCTION INPUTS, and FUNCTION OUTPUTS) discussed how you can write and use your own functions. This concept discusses how you can use OTHER PEOPLE'S functions to make your code much more powerful.

While you may not have realized it, you've already used several functions that other people have written. Some of those functions are included in the Python programming language that everyone (at least everyone who uses Python) has access to. For example, when you use the command print(), you are actually calling a function named "print" that comes standard with the Python language. You may think that print() requires just a single line of code, but what you don't see behind-the-scenes is that the print() function utilizes thousands of lines of code. You read that correctly -- every time you use print(), Python is running thousands of lines of code. But, because Python provides this command as a function, you can print output using a single programming line.

Now, in addition to thousands of functions that are provided as part of Python programming language, you also have access to the RaspberrySTEM API -- a set of functions that we (the RaspberrySTEM team) have written to make building with and using the RaspberrySTEM CREATOR Kit easier and more functional. For example, you might see the following in one of our projects:

Both of those lines of code are calling RaspberrySTEM functions that we have written to make using the RaspberrySTEM easier and more powerful. While you only needed to write two lines of code to setup the GPIO and turn on the LED, behind the scenes, there were over 300 lines of code that the RaspberrySTEM team has written to handle those tasks. Not to mention, within those 300+ lines of code, we were calling functions that other people had written that accounted for many thousands more lines of code!

And again, you were able to utilize all that code by writing just a single line.

Built-In Commands and Importing Functions/Modules

In our INTRODUCTION TO FUNCTIONS lesson, you saw how functions could be included in the same file as the rest of your program. But, many times, functions are separated into their own files. These files are called modules, and a module will generally contain many functions all related to a common task.

In Python, there are two specific ways functions are used:

Importing an Entire Module

If you're going to be using several functions within the same module, it's generally easiest to just import a module into your code. Doing this gives you access to any of the functions in that module any time you need them. We do this using the "import" command, and a good example that you've probably already seen is:

The module "time" contains functions for telling time, formatting time strings, working with time zones, etc. One function from the time module that we frequently use in our projects is sleep(). By importing that module, we now have access to all the functions within that module.

We now have access to the sleep() function by calling:

The function call above would sleep (pause) for 10 seconds.

Importing Individual Functions From a Module

In some cases, we don't need access to all the functions within a module -- we only need access to one or two. It's often convenient to import only the functions we need, directly by name. When we import in this way, we can directly use the function without prefacing it with the module name. For example, here is equivalent code to sleep for 10 seconds:

Throughout the projects, we'll be using a lot of functions. We'll make sure to point them out as we're using them, and hopefully you can begin to get an idea of how powerful functions and modules can be when writing complex code.

home