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 work slightly differently each time we use it.
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 a 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 write 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 concept 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:
A name
A birth year
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 birth year parameters, it might look like this:
"name" and "birth_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 show_my_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 2005. 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 2005, making it a number instead of a string. While we could have put quotation marks around 2005 to make it a string, we'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 1999, 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 of 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 are likely passed to the function would include:
Name of caller
Phone number of caller
Picture to be displayed for that caller
Ring tone to be used for that caller
Etc.
Before returning to the projects you may want to check out FUNCTION OUTPUTS.