Watch the video and take notes, pay attention and identify similarities and key differences between procedures functions.
Run and walk through the example (in code window).
Read the task and write code to solve.
Make a note of successful code in your book.
Answer questions in your book.
Watch the video and create notes in order to be able able to apply the knowledge given.
Area of a shape (2 functions and 1 procedure)
# Code Example functions (area of a shape)
def DataInput(): # function to gather user input
height = int(input("Enter the height of the shape :"))
width = int(input("Enter the width of the shape :"))
data = [height,width] # due to being limited to one value/structure return we put both values in a single array data structure sending two values in one data structure
return data # returning the data in the array data to the main program
def Calculation(data): # function to perform a calculation and return the result
area = data[0]*data[1] # data[0] is height, data[1] is width
return area # returning the data in area to the main program
def PrintOutput(area): # procedure to print out result
print("The area of the shape is :",area)
# MAIN PROGRAM #
print("--- Main program starts here ---")
data = DataInput() # calls DataInput function and sends no parameters data = is to store the returned data from the function
area = Calculation(data) # calls Calculation function sends data as a parameter and area = is store the data that returns from the funtion
PrintOutput(area) # calls PrintOuput procedure and it takes area as a parameter
print("--- Main program ends here ---")
Valid phone number (1 function)
# Code Example functions (valid phone number)
def Check(phoneNumber): # function check is sent the value held in phoneNumber as a passed parameter
length = len(phoneNumber) # find the number of characters in phoneNumber
if length == 11:
valid = True
else:
valid = False
return valid # return the value held in valid to the main program as a passed parameter
# MAIN PROGRAM
phoneNumber = str(input("Input your phone number :")) # inputs a users phone number
valid = Check(phoneNumber) # calls the Check function sending phoneNumber value as a passed parameter, valid = will store the result returned fromt the function
if valid == True:
print("This is a valid phone number")
else:
print("This is an invalid phone number")
Apply your knowledge of functions and write a program that will show your understanding of how to implement a function and use the returning data.
The program should implement a function called SquareCheck that will check a set of height and width values input from a user. The function will return True if the height and width are equal and False if they are not. A suitable message will be output to the user that states if the shape is square or not.
input height and width of a shape from a user
call the SquareCheck function, sending the height and width values as parameters
if the height is equal to the width then the shape is a square and the value True (Boolean) will be returned to the main program, otherwise False will be returned
if the Boolean value True is returned from the SquareCheck function then output a message "The shape is a square", if False it will output "The shape is not a square"
def SquareCheck(height,width):
if height == width:
square = True
else:
square = False
return square
# MAIN PROGRAM
height = int(input("Enter height of the shape :"))
width = int(input("Enter the width of the shape :"))
square = SquareCheck(height,width)
if square == True:
print("The shape is a square")
else:
print("The shape is not square")
State the similarities between procedures and functions.
State the differences between procedures and functions.
State how many values/structures you can return from a function.
Describe how you might return more than one value from a function.
Debug and complete the code below:
def MultipleOfTen( ______ ):
if ______ % 10 == 0:
______ = True
else:
______ = False
return ______
# MAIN PROGRAM
______ = int(input("Enter a number to check :))
______ = MultipleOfTen( ______ )
if ______ == True:
print("The number is a multiple of ten")
else:
print("The number is not a multiple of ten")
State the similarities between procedures and functions.
Procedures and functions both can have parameters passed to them when called.
State the differences between procedures and functions.
Procedures are sub programs that return no data to the calling program, functions return data to the calling program.
State how many values/structures you can return from a function.
one
Describe how you might return more than one value from a function.
When returning a more than one value from a function you cannot use variables. You can only return one value/data structure so append your data into an array and return the array (one data structure that contains more than one value).
Debug and complete the code below:
def MultipleOfTen( number ):
if number % 10 == 0:
multiple = True
else:
multiple = False
return multiple
# MAIN PROGRAM
number = int(input("Enter a number to check :))
multiple = MultipleOfTen( number )
if multiple == True:
print("The number is a multiple of ten")
else:
print("The number is not a multiple of ten")