LESSON 07 –
FUNCTIONS

 

 

LESSON NOTE/GUIDE

PROGRAM 1 – FUNCTION TO DISPLAY MESSAGE

·       Create a function that displays “hello” and then call it three times.


THE CODE

 

def showMsg():

    print("Hello")

 

# main program starts here

showMsg()

 


EXPLANATION


The above program outputs “Hello” to screen once.

In the first two lines, this program defines a function. 

 

In the first line, the keyword def starts off the function named showMsg.  All statements that are indented under this top line are part of the function. 

 

The second line, which is inside the function, prints “Hello”.  However, this line is only executed when the function is called.

 

The last line is part of the main program.  It calls the function by using the function name showMsg.  It is that this point that the statements in the function get called and outputs “Hello” to screen.

 

 

SIMILAR CODE

 

def showMsg():

    print("Hello")

 

# main program starts here

showMsg()

showMsg()

showMsg()

 


EXPLANATION

 

The above program outputs “Hello” three times to screen.

 

This code is similar to the above code but calls the function three times.  So, the word “Hello” gets outputted three times.

 

 

PROGRAM 2 – FUNCTION TO GREET

  • Create a function called greet that receives a name as parameter and outputs a greeting message to that name.


THE CODE

 

def greet(name):

    print("Hello", name)

 

# main program starts here

greet("Jay")

greet("Sonia")

 

 

EXPLANATION

 

The above message will output “Hello Jay” and then “Hello Sonia”.

 

The first two lines define the function named greet. 

 

Notice on the first line that this function has a variable called name inside the parentheses on the first line.  That variable is called a parameter (or argument).  To use this function, we must provide a value for that parameter.  That is why the function calls at the bottom both include a name that will be passed to the function.

 

When this function gets called, it outputs Hello name where name is the value of the provided parameter. 

 

Note that if you tried to call the function without a parameter in it, it will be an error.  In the next program, we will see how to get around this.

 

 

PROGRAM 3 – DEFAULT PARAMETER VALUES

·       Create a function called scary that gets a parameter called thing and outputs “I am afraid of thing”.   If the function gets called without a parameter value, make sure the parameter is set to “spiders”.

 


THE CODE

 

def scary(thing = "spiders"):
   
print("I am afraid of", thing)

# main program starts here
scary(
"nightmares")
scary()
scary(
"clowns")

 

 

EXPLANATION

 

The above code will output “I am afraid of nightmares”, then “I am afraid of spiders” and finally “I am afraid of clowns”.

 

This code is similar to the previous program except it provides a default value for the parameter named thing.  This default value only applies if the function is called with no value for that parameter.

 

 

PROGRAM 4 – RETURNING VALUES FROM THE FUNCTION

·       Create a function called add that gets 3 numbers as parameters and returns their sum.

 


THE CODE

 

def add(num1, num2, num3):
   
return num1 + num2 + num3

# main program starts here
print(add(3, 4, 1))

total = add(
3, 5, 2)
print(total)

 

 

EXPLANATION

 

The following program will output 8 and then 10.

 

The first two lines define the function add.  It received three parameters.  It then returns the sum of the three provided parameters.

 

When a function returns a value, that value replaces the function call in the line of code where the function was called from.

 

In the third line of code, the program prints what the add function returns.  As the function returns the total of 3 + 4 + 1 which is 8, it is 8 that is outputted.

 

In the fourth line of code, the variable total gets the value 10 that is returned from the function.  In the final line, 10 is outputted to screen.

 

 

 

PROGRAM 5 – MAX FUNCTION

·       Create a function called max that gets 2 numbers as parameters and returns the max value of the two.

 


THE CODE

 

def max(num1, num2):
   
if num1 > num2:
       
return num1
   
else:
       
return num2

# main program starts here
print(max(3, 5))
print(max(-2, -4))

 

 

EXPLANATION

 

The above code will output 5 and then -2.

 

At the top, the program has a 5-line function called max.  It receives two numbers as parameters.  The function will return the maximum value of the two.

 

In the main program section, we call the function twice and the value returned by the function calls gets printed to screen.

 


PROGRAM 6 – LEGAL MARK

·       Create a function called legal_mark that gets a mark as parameters and returns True if the mark is between 0 and 100.  Otherwise, it returns False.

 


THE CODE

 

def legal_mark(mark):

    if mark >= 0 and mark <= 100:

        return True

    else:

        return False

 

# main program starts here

m = int(input("Enter a mark"))

if (legal_mark(m) == True):

    print("Good")

else:

    print("Not a valid mark")

 

EXPLANATION

 

The above code asks the user for a mark and outputs Good if the mark is a valid mark.  Otherwise, it outputs Not a valid mark.

 

The function at the top gets the value of the mark as a parameter.  It simply checks that the mark is greater or equal to zero and less than or equal to 100.  If it is, then the mark is legal and the function returns True.  Otherwise, it returns False.

 


PROGRAM 7 – LIST TOTAL

·       Create a function called total that gets a list containing numbers as parameter and returns the total of all list elements.

 


THE CODE

 

def total(list):

    tot = 0

    for x in list:

        tot = tot + x

    return tot

 

# main program starts here

my_list = [2, 5, 1, 9]

print("The total is", total(my_list))

 

EXPLANATION

 

The function total gets a list as parameter.  It sets the variable tot to zero before looping through the entire list.  During each pass in the loop, the value of the current element is added to tot.  At the end, tot is contains the sum of all elements in the list and is returned.