Watch the video and take notes, pay attention to how programs can be split into subprograms and called from a main program.
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.
# This is a sub program that doesn't execute until called.
def Addition (number1,number2): # sub program that receives two values as passed parameters number1 and number2.
total = number1 + number2 # adds the received values and assigns the result to total
print(total) # output the value held in total
# Main program, this is where the program starts.
number1 = int(input("Enter first number :"))
number2 = int(input("Enter second number :"))
Addition(number1,number2) # calls the procedure (sub program) passing the parameters number1 & number2 to the procedure.
Apply your knowledge of procedures and write a program that will show your understanding of how to implement a procedure and the passing of parameters.
The program will prompt a user for their firstname,surname and age. The values from the variables will be sent to a procedure called screenoutput that will output them.
input, firstname,surname & age from a user and store the values in variables
call a the procedure screenoutput passing the parameters (the values from) firstname,surname & age
the screenoutput procedure will recieve the parameters (the values)
the values will be output in a suitable form
def screenoutput(firstname,surname,age): # procedure / sub-program
print("Name: ",firstname)
print("Surname :",surname)
print("Age :",age)
# Main program starts here
firstname = str(input("Enter your firstname :"))
surname = str(input("Enter your surname :"))
age = str(input("Enter your age :"))
screenoutput(firstname,surname,age)
Describe why a developer might use procedures when developing a program.
Define the term sub-program
State the purpose of passing parameters
Describe each part of the following code:
def subtract (number1, number2, number2):
Debug and complete the following code:
def bookcode ( ______ , ______ ):
______ = year[1:]+title[:4]
print("The book code is :",code)
# MAIN PROGRAM
title = str(input("Enter the title of the book :"))
year = str(input("Enter the year of publication YYYY :"))
bookcode( ______ , ______ )
Describe why a developer might use procedures when developing a program.
A developer might use sub programs like procedures when tackling a large difficult problem. The developer would split a program up using decomposition and tackle each sub program one at a time. Because of the nature of small problems the developer would complete and test each sub program before adding it to the main program. This process would also allow large teams of developers to work on the same project at one time each tackling a sub program until complete.
Define the term sub-program
Small self contained program that has a defined purpose. / A small program to complete definite stage of the overall program.
State the purpose of passing parameters
To pass the value of a variable to a procedure for use in the sub program.
Describe each part of the following code:
def subtract (number1, number2, number2):
define the procedure with the name "subtract" ( passed parameters in order separated by commas , ):
Debug and complete the following code:
def bookcode ( title , year ):
code = year[1:]+title[:4]
print("The book code is :",code)
# MAIN PROGRAM
title = str(input("Enter the title of the book :"))
year = str(input("Enter the year of publication YYYY :"))
bookcode( title , year )