Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Subprograms

What is a subprogram
It is a self-contained module of code that performs a specific task.

What is the benefit of subprogram?

Subprogram

Function Procedure
A block of code which only runs when it is Allows us to group a block of code under a
called. name
Can pass data known as parameters or Can pass the parameters and arguments
arguments into a function
Can return data Can't return data

Function
Pseudocode Description Python
FUNCTION dice () def dice ()
SET simdie TO RANDOM simdie = random.radint(1,7)
(6) return simdie
RETURN simdie
END FUNCTION

Calling the function in main diethrow = dice ()


program

diethrow = dice ()
Procedure
Pseudocode Python
PROCEDURE dice () def dice ()
SET simdie TO RANDOM (6) simdie = random.radint(1,7)
SEND simdie TO DISPLAY print(simdie)
END PROCEDURE

What are parameters?


The names of the variables that are used in the subroutine to store the data passed from
the main program as arguments.
Ex: FUNCTION rectangle (length, width)
Length and width are the parameters

Same in procedure PROCEDURE rectangle (length, width)


What is a scope
The region of code within which a variable is visible.
Local variable – A variable that is accessed only from within the subprogram in which it is
created.
The variable only exits within the function and is referred to as a local variable
Ex: simdie
Global variable – A variable that can be accessed from anywhere in the program, including
inside subprograms.
Variable in the main program
Ex: diethrow
Functions and procedures are useful when using menus in a program.
When a user selects a menu option, they can be sent to a particular function or procedure.
What is built-in function?
Functions that are provided in most high-level programming languages to perform common
tasks.
These are designed to save the programmer time, such as functions that print, count the
number of characters in a string and generate random numbers.

You might also like