SUB PROGRAMS
Higher Computing Science
MODULAR CODE
• Computer programs are much easier to write and understand if they are
broken down into smaller blocks. These blocks are called sub-programs
or subroutines.
• Large programs will have teams of developers working on them. Using
sub-programs makes it easier to develop and maintain code.
SUB-PROGRAMS
• Independent block of code within the main program.
• Carries out a specific task e.g.
• calculate_area
• display_total
• move_player
• update_score
• Can be programmed and tested independently of other sub-programs.
SUB-PROGRAMS
• Each sub-program begins with a declaration which includes:
• the sub-program name
• a list of formal parameters being passed into the sub-program
FUNCTIONS
• A function is a sub-program which returns a value
PROCEDURES
• A procedure is a sub-program which produces an effect
PARAMETER PASSING
• Parameters are items of data that can be passed from the main program
to sub-programs.
Main
Program
Sub-program
Parameter
PARAMETER PASSING: PSEUDOCODE
FUNCTION calc_rectangle_area(length, breadth)
area = length * breadth
RETURN area
END FUNCTION
area_rect_1 = calc_rectangle_area(10, 4)
display area_rect_1
area_rect_2 = calc_rectangle_area(3, 6)
display area_rect_2
PARAMETER PASSING: PYTHON
FORMAL PARAMETERS
• Formal parameters are identified in the sub-program definition.
Formal
parameters
FORMAL PARAMETERS
• Actual parameters are data items that are passed to the sub-program.
Actual
parameters

Sub-programs