Signed up recently but had issues with the confirmation email?
Click Forgot password and follow the reset steps. You can choose the same password you used when you signed up. This should then provide access.
Remember to check Spam and Junk. The email will be from auth@progressbytes.co.uk.
How to use sub programs (functions and procedures) to produce structured code
2.2.3 Additional Programming Techniques
Practise exam questionsAdditional Programming Techniques — Note 8 of 13
The difference and use of functions and procedures and where to use them effectively
Sub-programs (also called sub-routines) are reusable blocks of code that perform a specific task. There are two types: functions return a value, while procedures perform a task without returning a value. Using sub-programs makes code modular, reusable, and easier to test and maintain.
return keyword.printArea(width * height) passes ONE value. Arguments are separated by commas: printArea(width, height) passes two.totalPay = calculatePay(experience, miles).greet("player1"). A function call is used inside an assignment or expression, because it gives back a value: total = add(5, 3).// FUNCTION: returns a value
function hireCost(hours, boat)
if boat == "kayak" then
cost = 12 * hours
else
cost = 20 * hours
endif
return cost // RETURN, not print
endfunction
// PROCEDURE: does not return a value
procedure greet(name)
print("Hello " + name)
endprocedure
// Calling them
result = hireCost(3, "canoe") // the returned value is stored
print(result) // 60
greet("player1") // a procedure call on its own line# FUNCTION - returns a value
def hireCost(hours, boat):
if boat == "kayak":
cost = 12 * hours
else:
cost = 20 * hours
return cost # RETURN, not print
# CALLING the function:
result = hireCost(3, "canoe")
print(result) # Output: 60
# PROCEDURE - does not return a value
def greet(name):
print("Hello, " + name + "!")
greet("player1") # Output: Hello, player1!# Function: returns a value
def add(x, y):
return x + y
total = add(5, 3) # the returned value is stored
print(total) # 8
# Procedure: does a job, returns nothing
def greet(name):
print("Hello", name)
greet("player1") # outputs: Hello player1hours and boat are given as parameters, USE them — don't do hours = input() inside the function.cost = hireCost(2, "kayak").return total, not print(total), inside the function.freeseats("Red"), not freeseats(Red). Store the result: redseats = freeseats("Red").result = hireCost(3, "canoe") then print(result).totalPay = calculatePay(experience, miles) then print(totalPay).Ready to test yourself?
Practise 2.2.3 questions