External Exam Download Resources Web Applications Games Recycle Bin

Modularisation

Pseudocode always starts and ends with the BEGIN and END keywords:

main algorithm (there should only be 1)

BEGIN
  statements
END

procedures, subroutines, methods or functions

BEGIN name
  statements
END
BEGIN findMax(a,b)
  IF a > b THEN
    RETURN a
  ELSE
    RETURN b
  ENDIF
END

BEGIN findDifference(n1,n2)
  IF findMax(n1,n2) == n1 THEN
    RETURN n1 - n2
  ELSE
    RETURN n2 - n1
  ENDIF
END

BEGIN 
  INPUT num1
  INPUT num2
  difference = findDifference(num1, num2)
  OUTPUT difference
END

Note the use of global variables num1, num2 which can be accessed globally in the entire program, as well as local variables (n1 & n2 in findDifference, a & b in findMax) which can only be accessed in their respective functions:

difference.py

def findMax(a,b):
  if a > b:
    return a
  else:
    return b

def findDifference(n1,n2):
  if findMax(n1,n2) == n1:
    return n1 - n2
  else:
    return n2 - n1

num1 = int(input("num1: "))
num2 = int(input("num2: "))
difference = findDifference(num1, num2)
print(difference)