INTRODUCTION TO FUNCTIONS

In this project we’re going to learn about functions and modules. A function is one of the most basic concepts in programming, and also one of the most important. It often takes a lot of code to get a computer to do even a simple task. And if you want the computer to do a simple task over and over again, that could mean having to write lots of code over and over again.

That doesn’t sound very efficient, does it?

It’s not, which is why instead of writing the same code many times, we have the ability to just write the code once in a special format, and then we can use that code over and over again, as many times as we want. That special format that allows us to re-use the code over and over again is called a function.

Here’s an example…

Let’s say that we wanted to write some code that would print my name and the year I was born. We could do this exactly like we did with the “Hello, World!” code we wrote in Project #1, and our code might look like the following:



Now let’s say we expected to print those same lines many times within our code. While we could type those lines over and over again in the code, that would …

The simpler way would be to create a function that prints those two lines, and then “call” the function every time we want those lines printed. Let’s turn the above two lines into a function that we can call from other places in our code.

The first step in creating a function is giving it a name. In this case, let’s say we wanted to call this function, “print_info”. This is done by using the “def” keyword on the first line of the function, followed by the function name, a set of parentheses and a colon:



The keyword "def" stands for "define," as that's what we're doing -- defining a new function.  But, as we'll explain down at the bottom of this lesson, the computer isn't going to run this code until the function is "called."

After the first line that names the function comes the code that we want our function to run every time it is called. In this case, it’s the two print lines we discussed earlier. 

So, this is what our function would look like:



Notice that all the code lines of the function are indented. This is required, and is how the code knows which lines are associated with which function.

To now use that function, we can "call it" (have the computer execute those lines of code) elsewhere in our program...like this:



To call the function, all we have to do is to provide the function name followed by a set of parentheses.

Let’s put it all together and write a program that defines this function and then calls this function to actually have it print out those two lines:



One big value in functions is that it allows us to reuse the same code over and over. So, let’s see what happens when we call that function several times in a row:



If you run this program, you’ll see that each of the three times the function is called, it prints the two lines. Imagine if we were going to need those two lines printed a hundred times throughout our program – you can see how using a function is a lot simpler and also makes our code much less complicated.

Note: We mentioned earlier that when you run a program, the computer will execute each line of code, starting at the top and moving down through the code, line-by-line. But, when the computer sees a function, it doesn’t execute that code until the function is called. So, for our code above, the first three lines (the function definition and the two lines of code in the function) are ignored and the computer will start by executing the first line below the function. To make things simpler, functions are often stored in separate files, so that the code is easier to read and follow.

There's more to functions than what we've covered here...we highly recommend reading FUNCTION INPUTS before moving on...