FUNCTION INPUTS

Now that we have a basic idea of what a function is and how it works, let's talk about one of the more powerful aspects of using functions.  Oftentimes, functions will contain code that we want to use over and over again, but we’re going to want the code to do slightly different things based on other things going on in our software.

For example, when your cell phone rings, the software inside your phone is doing a lot of complicated stuff to generate the ring. Your phone has to “wake up,” turn on the back light, print information on the display, check to see if you’re pushing any buttons, play ringtone, etc. In fact, the code that makes your cell phone ring may contain a hundred or more lines of code.

When different friends call, you’re going to see different information on your screen. For your friend John, you might see John’s name, phone number and picture. For your friend Allison, you might see Allison’s name, phone number and picture. Does this mean that the software has to have a hundred lines of code for John and a different hundred lines of code for Allison (as well as a different hundred lines of code every other person in your contact list)?

Luckily, no…

With functions, we have the ability to do generic tasks (like generate a ring), while also changing based on specific information (like putting different information on the screen for different callers). This is done by sending “inputs” (also called “parameters”) to the function. Parameters are just pieces of information that the function uses to customize the task it is performing.

Let’s go back to our previous project where we printed two pieces of information on the screen – my name (Jason) and the year I was born (1971). What if we wanted to print a name and birth year for other people as well – people with a different name and birth year than mine? To do this, we modify our function to receive parameters and to use those parameters to customize what the function does.

In this example, our function is going to take two parameters:

We tell the function what parameters to expect by listing them between the parentheses in the first line of the function (the line that names the function). Each parameter has its own name, which should be descriptive of what the parameter is.

For example, if we wanted to modify our function from the previous project to take a name and a birth year parameters, it might look like this:

“name” and “year” are now variables that we set when we call the function, and those variables can be used within that function:

Now, when we call the print_info function, we will always supply two parameters to the function – a name and a year.

For example, let’s say we have a friend named Bob and he was born in 1992. We can send his information to the function like this:

Notice that we put quotation marks around “Bob” to make it a string (as you would expect), but we left the quotation marks off of 1992, making it a number instead of a string. While we could have put quotation marks around 1992 to make it a string, I’ve decided to make it a number for reasons that will become clear later on.

As another example, let’s say we have a friend named Susie who was born in 1985, and we want to print her information as well:

Now, let’s put it all together, and see what our code would look like if we had this new function and we called this function twice – once to print Bob’s information and once to print Susie’s information:

You may have noticed that this example is very similar to our cell phone example above. The software in a cell phone likely handles a ring by calling a function – the parameters to this function would be all the information that the function needs to correctly display the caller’s information on the screen and make the phone ring. So, the parameters that likely get sent to the function would include:

We realize that functions aren't the most exciting topic in the world, but there is another function topic that's important to understand before getting too far into the projects.  If you're not familiar with them, you should check out Function Outputs before getting back to the projects...