Functions

CT03d

  • Define the term 'function’
  • Define the term 'parameter’
  • Define the term 'return value’
  • Create functions

Repeating Ourselves?

CT03d

In your last lesson, you learned about procedures.

These are blocks of code that carry out an action. They are defined before the main program.

Today, you are going to look at the other kind of subprogram: functions.

Functions

CT03d

A function is a subprogram.

Functions are created in the same way as procedures, using the def command before the main program. 

They can have input parameters, just as procedures can.

The only difference is that a function has a return value.

When a function completes its task, the result is returned to the calling code.

Functions

CT03d

You have used Python built-in functions before. There are lots of them!

Examples include:

userChoice = input("Enter your choice: ")
attendance = float(“0.85”)
missedAssignments = int(“1”)
print(len(exampleString))
print(exampleString.isalpha())
print(exampleString.lower())

Return values

CT03d

In the examples on the previous slide, each function returns something.  This is what makes them functions.

In our examples, each function call is assigned to a variable or is printed. This is where the return value ends up.

But what does this actually mean?

It means that once the function has finished its task, a value is sent back.

This return value is sent back to the line in the program from which it was called.

Practice I

CT03d

Decide whether each of the following is a procedure or a function. If it is a function, then give its return value.

input ("Enter Y or N ") 

Code

Function

Function or
Procedure?

What the user types, as a string

Return value?

myList.append("Wilma")
myList.append("Fred")
list(range(5))
print("Welcome to my program")
len("Hello World")
int(input("Enter your age in years "))
chr(65)
ord("A")
random.randint(1, 6) 
del myList[0] 

Practice I

CT03d

Decide whether each of the following is a procedure or a function. If it is a function, then give its return value.

input ("Enter Y or N ") 

Code

Function

Function or
Procedure?

What the user types, as a string

Return value?

myList.append("Wilma")

Procedure

myList.append("Fred")
list(range(5))
print("Welcome to my program")
len("Hello World")
int(input("Enter your age in years "))
chr(65)
ord("A")
random.randint(1, 6) 
del myList[0] 

Function

Function

Function

Function

Function

Procedure

Procedure

Procedure

Function

11

What the user types, converted to an integer.

A

65

[0, 1, 2, 3, 4]

A random integer between 1 and 6 inclusive.

User-defined functions

CT03d

Function definitions are the same as procedure definitions, with one extra part – they must return a value of some kind.

The return value.

The function call is assigned to a variable so that the return value can be used.

# Constants
VAT = 0.2

# Global variables
subtotal = 34.99
finalBill = 0.0

# Subprograms
def addVAT (bill):
    total = 0.0
    total = bill + (bill * VAT)
    return (total)

# Main program
finalBill = addVAT(subtotal)
print (finalBill)

Functions look the same as procedures.

Variables and subprograms

CT03d

You might have noticed that there are a lot of variables in this example.

# Constants
VAT = 0.2

# Global variables
subtotal = 34.99
finalBill = 0.0

# Subprograms
def addVAT (bill):
    total = 0.0
    total = bill + (bill * VAT)
    return (total)

# Main program
finalBill = addVAT(subtotal)
print (finalBill)

It is important to understand that the variables inside a subprogram do not exist outside of it.

Think of subprograms as black boxes. Arguments go in, return values may come out. What happens inside cannot be accessed by the rest of the program.

Giving different names to variables inside and outside of subprograms helps make debugging much easier.

Practice II

CT03d

A program simulates the roll of a dice, but the user can determine the number of sides on the dice.  

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
myRoll = polyhedraDice(numSides) 
numSides = 0 
myRoll = 0 
roll = 0 
status = True 
status = False  # Never a rebel 
def polyhedraDice (pSides): 
def isRebel(numSides): 
roll = random.randint (1, pSides) 
return (roll) 
return (status) 
import random 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.") 
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll)) 
numSides = int (input ("Enter the number of sides on your die: "))

These kinds of dice are called ‘Polyhedra’ dice.  The lines of code below are all jumbled up. Arrange them in order.

CT03d

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
myRoll = polyhedraDice(numSides) 
numSides = 0 
myRoll = 0 
roll = 0 
status = True 
status = False  # Never a rebel 
def polyhedraDice (pSides): 
def isRebel(numSides): 
roll = random.randint (1, pSides) 
return (roll) 
return (status) 
import random 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.") 
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll)) 
numSides = int (input ("Enter the number of sides on your die: ")) 
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------ 

CT03d

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
myRoll = polyhedraDice(numSides) 
numSides = 0 
myRoll = 0 
roll = 0 
status = True 
status = False  # Never a rebel 
def polyhedraDice (pSides): 
def isRebel(numSides): 
roll = random.randint (1, pSides) 
return (roll) 
return (status) 
#import random 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.") 
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll)) 
numSides = int (input ("Enter the number of sides on your die: ")) 
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------ 

CT03d

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
myRoll = polyhedraDice(numSides) 
numSides = 0 
myRoll = 0 
roll = 0 
status = True 
status = False  # Never a rebel 
#def polyhedraDice (pSides): 
#def isRebel(numSides): 
roll = random.randint (1, pSides) 
return (roll) 
return (status) 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.") 
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll)) 
numSides = int (input ("Enter the number of sides on your die: ")) 
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
  
def isRebel(numSides): 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------ 

CT03d

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
myRoll = polyhedraDice(numSides) 
numSides = 0 
myRoll = 0 
roll = 0 
status = True 
status = False  # Never a rebel 
roll = random.randint (1, pSides) 
#return (roll) 
#return (status) 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.") 
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll)) 
numSides = int (input ("Enter the number of sides on your die: ")) 
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
  return (roll)
  
def isRebel(numSides): 
  return (status) 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------ 

CT03d

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
myRoll = polyhedraDice(numSides) 
numSides = 0 
myRoll = 0 
roll = 0 
status = True 
status = False  # Never a rebel 
roll = random.randint (1, pSides) 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.")
#print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll)) 
#numSides = int (input ("Enter the number of sides on your die: ")) 
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
  return (roll)
  
def isRebel(numSides): 
  return (status) 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------
numSides = int (input ("Enter the number of sides on your die: "))
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll))

CT03d

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
#myRoll = polyhedraDice(numSides) 
numSides = 0 
myRoll = 0 
roll = 0 
status = True 
status = False  # Never a rebel 
roll = random.randint (1, pSides) 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.")
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
  return (roll)
  
def isRebel(numSides): 
  return (status) 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------
numSides = int (input ("Enter the number of sides on your die: "))
myRoll = polyhedraDice(numSides)
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll))

CT03d

if (isRebel (numSides)): 
else: 
if ((numSides < 4) or (numSides > 8)): 
roll = 0 
status = True 
status = False  # Never a rebel 
roll = random.randint (1, pSides) 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.")
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
numSides = 0 
myRoll = 0
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
  return (roll)
  
def isRebel(numSides): 
  return (status) 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------
numSides = int (input ("Enter the number of sides on your die: "))
myRoll = polyhedraDice(numSides)
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll))

CT03d

if (isRebel (numSides)): 
else: 
roll = 0 
roll = random.randint (1, pSides) 
print ("You're a dice rebel, but I'll roll that dice for you!!") 
print ("You're a bit conservative, but you can still have a roll.")
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
numSides = 0 
myRoll = 0
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
    return (roll)
  
def isRebel(numSides): 
    status = False  # Never a rebel 
    if ((numSides < 4) or (numSides > 8)): 
        status = True 
    return (status) 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------
numSides = int (input ("Enter the number of sides on your die: "))
myRoll = polyhedraDice(numSides)
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll))

CT03d

roll = 0 
roll = random.randint (1, pSides) 
# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
numSides = 0 
myRoll = 0
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
    return (roll)
  
def isRebel(numSides): 
    status = False  # Never a rebel 
    if ((numSides < 4) or (numSides > 8)): 
        status = True 
    return (status) 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------
numSides = int (input ("Enter the number of sides on your die: "))

if (isRebel (numSides)): 
    print ("You're a dice rebel, but I'll roll that dice for you!!") 
else: 
    print ("You're a bit conservative, but you can still have a roll.")

myRoll = polyhedraDice(numSides)
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll))

CT03d

# ------------------------------------------------------------ 
# Import libraries 
# ------------------------------------------------------------
import random  
# ------------------------------------------------------------ 
# Global variables 
# ------------------------------------------------------------ 
numSides = 0 
myRoll = 0
# ------------------------------------------------------------ 
# Subprograms 
# ------------------------------------------------------------ 
def polyhedraDice (pSides):
    roll = 0 
    roll = random.randint (1, pSides)
    return (roll)
  
def isRebel(numSides): 
    status = False  # Never a rebel 
    if ((numSides < 4) or (numSides > 8)): 
        status = True 
    return (status) 
# ------------------------------------------------------------ 
# Main program 
# ------------------------------------------------------------
numSides = int (input ("Enter the number of sides on your die: "))

if (isRebel (numSides)): 
    print ("You're a dice rebel, but I'll roll that dice for you!!") 
else: 
    print ("You're a bit conservative, but you can still have a roll.")

myRoll = polyhedraDice(numSides)
print ("Sides = " + str(numSides) + "  Roll = " + str(myRoll))

Review

CT03d

  • Define the term 'function’.
    • Similar to a procedure: a block of code defined before the main program that can be reused. The difference is that functions return a value.
  • Define the term 'parameter’.
    • A value passed into a function or procedure.
  • Define the term 'return value’.
    • The output of a function.
  • Create functions.
    • Using the def keyword and the return keyword.

ct03d Functions

By David James

ct03d Functions

  • 359