FUNCTIONS

In all computer programming languages, a function (also known as a procedure or method) can be defined as a named block code that performs a specific task. Functions are normally created for sections of code that the programmer wants to use over and over again. 

Let’s not only look deeper into this concept but get some 90s nostalgia going at the same time shall we.

In 1994, the rap group Kid ‘n’ Play starred in the movie House Party 3 (yes… there were two before it). In that movie they sang the song ‘Make Noize’. Relieve the awesomeness below.

In the main chorus, Kid ‘n’ Play tell everyone that when they say ‘Make Some’ everyone should say ‘Noise’. That’s a function! They say a combination of words (‘Make Some’) and a series of actions take place (in this case the audience says ‘Noise). Let’s see what that would look like as a Python program

[python]#Define function makeSome<br />def makeSome():
     output("Noise!")
#Call on function makeSome
makeSome()<br />[/python]

Don’t worry if this doesn’t make sense just yet. It will soon enough.

Functions Syntax

Whether you know it or not, you’ve been using functions so far in this course. For instance, you have been using the built-in functions listed below:

[python]
print() #outputs whatever is in the brackets to the screen
input() #accepts input from the keyboard
int() #converts whatever is in the brackets to an integer
range() #allows values to be put in brackets an create a range within those values
[/python]

 

 

So if those are built-in functions, how do I create my own? Simply follow the setps below.

STEP 1: CREATE A FUNCTION DEFINITION

Much like in the video above, we need to define what should happen when the function is called. For example, When I say ‘Make Some’, you say ‘Noise’. The syntax for defining a function is as follows:

[python]&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;def FUNCTIONNAME():&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;     statements&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;[/python]

Here, FUNCTIONNAME is whatever you want it to (follows the same rules as a variable name). This is what will be used to call on your function later. Also, notice the brackets at the end of the function name. This will be important later.

STEP 2: CALL ON YOUR FUNCTION

Now, you can call on your function using the code below:

[python]&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;FUNCTIONNAME()&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;[/python]

Once again, notice the brackets at the end of the function call. We will need this later.

Sample Code

In this sample, we will create a function for the song ‘I Fought the Law’ by the Bobby Fuller Four. If you don’t know it, you can hear the song on YouTube. Copy the code below and execute it. Does it create the result you expected? Notice that the function code only runs when called upon.

[python]&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;#Define the function 'chorus', which will print out the main verse&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;def chorus():&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;    print(&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;\nI fought the law and the law won&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;)&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;    print(&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;I fought the law and the law won\n&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;)&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;print(&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;Breaking rocks in the hot sun&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;)&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;chorus() #Runs the 'chorus' code&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;print(&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;I needed money 'cause I had none&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;)&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;chorus() #Runs the 'chorus' code
[/python]

Check Your Understanding

Before moving to the next section, let’s see how well you know the current material.

  1. Create a program that will store 5 grocery items into a list called groceries. Print the list as a whole as well as each individual item.
  2. Create a program that will store 6 names into a list. Your program should then print the names in forward and reverse order. For instance, the list Fatima, John, Sandra, Mohamed, Teresa, and Shawn should print:
    • Fatima, John, Sandra, Mohamed, Teresa, Shawn
    • Shawn, Teresa, Mohamed, Sandra, John, Fatima