USING FUNCTIONS & MODULES

The last couple projects discussed many of the important aspects of functions and how you can write functions to make your code easier to write, easier to read and more reusable.  This project is going to discuss 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 things to output using a single programming line.

Now, in addition to hundreds of functions that are provided by the Python programming language, you also have access to many dozens of functions that we (the RaspberrySTEM team) have written to make building with and using the RaspberrySTEM kit easier and more functional.  For example, in the GPIO project (Project #6), your program included the following lines of code:

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 hundreds 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 an entire 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 already seen is as follows:

The module "rstem" contains all of the functions that we provide specially for the RaspberrySTEM kit projects.  By importing that entire module, we now have access to all the functions within that module.  As an example, within that module is a set of functions called "gpio" functions, and within the set of "gpio" functions is a specific function called Output().  If you recall from our GPIO project, this function is used to configure a GPIO as an output.  

Once we import the entire rstem module, we can get access to the Output() function by calling:

And because the Output() function takes a single input (the GPIO number we are using), we'll call the function using that single function input.  In our GPIO project, we were using GPIO14, so to set that GPIO to be used as an output, we used the following:

Because we imported the entire rstem module, we could just as easily call any of the other functions in that module using a similar approach.

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.  While we could still import the entire module, there are some situations where importing just the one or two functions we want access to makes more sense.  For example, let's say that we only needed access to one function in the rstem module (we can stick with the Output() function that we've been using). 

To import just a single function from a module, we use the "from...import" command.  For example, to import just the Output() function from the rstem module, we would do the following:

One of the big benefits of importing this specific function (as opposed to the entire module) is that we can now call the Output() function using the following command:

You probably noticed that calling that function now requires a whole lot less typing than it did above when we imported the entire module.  That's the tradeoff -- by importing the entire module, you have access to all the functions in the module, but by importing a single function, it requires a lot less information to call that function in your code.

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.