Subprogram
Subroutines/Subprogram
is a block of code that preforms a specific task but does not represent the entire
program.
When a subroutines/subprogram is called, the calling program is halted and control
is transferred to the subroutine.
After a subprogram has completed execution, the control is passed back to the
calling program
Program
Subprogram Subprogram Subprogram
Subroutines/Subprogram
User defined subroutines/subprogram
Two main type of subroutine exist:
Procedure
are small section of code that can be reused.
Do not return a value.
A parameter allows a value to be passed in to the procedure.
Procedure Pseudocde Python
Syntax PROCEDUERE identifier( Parameter) def identifier (Parameter) :
Code Code
ENDPROCEDURE
Parameter :the names of the variable that are used in the
subprogram to the data passed from the main program as
argument.
Procedure without parameter
Write a procedure that print five “*” .
Pseudocode Python
PROCEDURE Star def Star ()
OUTPUT “*****” print( “*****” )
ENDPROCEDURE
Star ()
CALL Star
SubProgram (Proecedure)
Main Program
PROCEDURE Star
OUTPUT “*****” CALL STAR
ENDPROCEDURE
Procedure without parameter
Write a procedure that take the number as parameter and print the
given number of *
Pseudocode Python
PROCEDURE Star : INTEGER) def Star (num)
FOR Count (1 Num
TO Num for count in range (num):
OUTPUT “*” print( “*” )
NEXT Counter
ENDPROCEDURE
CALL Star (10) Star (10)
SubProgram (Precedure)
Main Program
PROCEDURE Star (Num : INTEGER)
FOR Count 1 TO Num CALL STAR (10)
OUTPUT “*”
10
NEXT Counter
ENDPROCEDURE
Subroutines/Subprogram
User defined subroutines/subprogram
Function
are similar to procedure
The difference is that the functions have one or more values passed to them
and one or more values are returned to the main program.
A parameter allows a value to be passed in to the procedure.
Procedure Pseudocde Python
Syntax FUNCTION identifier( Parameter) RETURN Datatype def identifier (Parameter) :
Code Code
RETURN value return value
ENDFUNCTION
Function
Write a function that convert a centimeter to meter
Pseudocode Python
Function Meter ( cm : INTEGER) RETURN def Meter (cm)
REAL ans = cm/100
Ans cm / 100
return ans
RETURNAns
ENDFUNCTION
mvalue = Meter (10)
Mvalue Meter (10)
Function
FUNCTION Meter ( Cm : INTEGER ) RETURN
REAL
Ans Cm / 100
RETRUN Ans
10 0.1
Mvalue Meter (10)
Exercise
1. Write a procedure that convert the given temperature from Fahrenheit to
Celsius and print result temperature ( c=( f-32) * (5/9) )
Write a function that convert the given temperature from Celsius to
Fahrenheit . (f =( c * (9/5)) + 32)
Write a program which output the following menu
1. Perimeter of Circle
2. Perimeter of Rectangle
3. Perimeter of Triangle
4. Exit
Ask the option of user as the input and
Process according to the user’s option
This pseudocode must use procedure or function
Library SubProgram
Predefined subprogram/ library subprogram
Libraries contain pre-written, tested functions
MOD – return remainder of a division
Res MOD (5,2) // 1
DIV – return the whole number part of division
Res DIV ( 5,2) //2
ROUND – returns the value of rounded to a given number of decimal places
Res ROUND( 1.2345 , 2) // 1.23
RANDOM – return random number between 0 and 1
Res RANDOM ( )
Library Subprogram
Predefined subprogram/ library subprogram
Many programming languages include built-in, ready-made functions, such as:
int(value) eg. int( 3.5)
input( String) eg. input(“Enter a number”)
print( String) eg. Print(“hello”)
Additionally, some languages allow functions to be added in from external files
called libraries.
Libraries contain pre-written, tested functions
Advantages of Using Subroutines
The subprogram can be called when needed:
A single block of code can be used many times in the entire program, avoiding the need
for repeating identical code.
This improves the modularity of the code, make is easier to understand and helps in the
identification of errors
There is only one sections of code to be debug:
If an error is located in a subprogram ,only the individual subroutine needs to be
debugged.
There is only one section of code to update:
Improvements and extensions of the code are available everywhere the subprogram is
called
Local and Global variable
Local variable : a variable that is accessed only from within the
subprogram in which it is created
Global variable: a variable that can be accessed from anywhere in the
program, including inside subprogram
Scope of variable : the region of code within which a variable is visible
Example
DECLARE Number1, Number2, Answer : INTEGER
PROCEDURE Test Global
DECLARE Number3, Answer : INTEGER Number1 = 10
Number1 ← 10 Number2 =20
Number2 ← 20 Answer = 150
Number3 ← 30
Answer ← Number1 + Number2
Local (Test)
OUTPUT "Number1 is now ", Number1 // 10
OUTPUT "Number2 is now ", Number2 // 20 Number3 =30
OUTPUT "Answer is now ", Answer // 30 Answer = 30
ENDPROCEDURE
Number1 ← 50
Number2 ← 100
Answer ← Number1 + Number2
OUTPUT "Number1 is ", Number1 // 50
OUTPUT "Number2 is ", Number2 // 100
OUTPUT "Answer is ", Answer // 150
CALL Test
OUTPUT "Number1 is still ", Number1 // 10
OUTPUT "Number2 is still ", Number2 // 20
OUTPUT "Answer is still ", Answer // 150
OUTPUT "Number3 is ", Number3 // Error
DECLARE a,b, c : INTEGER
PROCEDUR Example
BEGIN PROCEDURE Global var
a=100
DECLARE a , b , ans: INTEGER
b=200
a 10 C=40
b 30
Local var (Example)
c a + b a= 10
ans c b=30
OUTPUT a , b, c
Ans= 40
END PROCEDURE
a 100
b 200
c a + b
OUTPUT a , b , c
CALL Eample
OUTPUT a, b, c //100,200,40
StudentID [ 8, 9,2,1,3]
StudentName [ “Mya”, “Kyaw”,”Aung”,”Thinzar”,”Kaung”]
PROCEDURE SortID ()
BEGIN PROCEDURE
DECLARE Size : INTEGER
Size = 5
WHILE Size > 1 DO
// implement pass
FOR Index 1 TO Size -1
IF StudentID [Index ] > StudentID[index +1] THEN
Temp StudentID[index]
StudentID[index] StudentID[index +1]
StudentID[index+1] Temp
Name StudentName[index]
StudentName[index] StudentName[index +1]
StudentName[index+1] Name
ENDIF
NEXT Index
Size Size -1
ENDWHILE
FOR Index 1 TO 5
OUTPUT StudentID[Index] , StudentName[Index]
StudentID [ 8, 9,2,1,3]
StudentName [ “Mya”, “Kyaw”,”Aung”,”Thinzar”,”Kaung”]
PROCEDUER SortID
Last = 4
// pass
REPEAT
Swap False
FOR index 0 T0 Last -1 DO
IF StudentID [index] > StudentID[ index+1] THEN
Temp StudentID[ index]
StudenID [index] StudentID[index+1]
StudentID[index+1] Temp
TempName StudentName[index]
StudentName[index] StudentName[index+1]
StudentName[index+1] TempName
Swap True
ENDIF
NEXT index
Last Last -1
UNTIL Swap = FASLE OR Last = 0
Sorting Algorithm
Pass 1
Bubble sort (Ascending Order)
1. Start at the beginning of the list 4 2 6 1 3
2. Compare the values in the position 0 and
position 1 in the list – if they are not in 2 4 6 1 3
ascending order then swap them
3. Compare the values in the position 1 and 2 4 6 1 3
position 2 in the list and swap if
necessary 2 4 1 6 3
4. Continue to the end of the list
2 4 1 3 6
5. If there have been any swaps repeat step
1 to 4
Last = 4
Sort 4 2 6 1 3 in ascending order
using bubble sort
Sorting Algorithm
2 4 1 3 6 2 1 3 4 6
1 2 3 4 6
2 4 1 3 6
2 1 4 3 6 1 2 3 4 6
2 1 3 4 6 Pass 3
2 1 3 4 6
Pass 2