SlideShare a Scribd company logo
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)
Code
ENDPROCEDURE
def identifier (Parameter) :
Code
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
PROCEDURE Star
OUTPUT “*****”
ENDPROCEDURE
CALL STAR
Main Program
Write a procedure that print five “*” .
Pseudocode
PROCEDURE Star
OUTPUT “*****”
ENDPROCEDURE
CALL Star
SubProgram (Proecedure)
Python
def Star ()
print( “*****” )
Star ()
Procedure without parameter
PROCEDURE Star (Num : INTEGER)
FOR Count  1 TO Num
OUTPUT “*”
NEXT Counter
ENDPROCEDURE
CALL STAR (10)
Main Program
Write a procedure that take the number as parameter and print the given
number of *
Pseudocode
PROCEDURE Star
FOR Count  1 TO Num
ENDPROCEDURE
CALL
SubProgram (Precedure)
Python
def Star (num)
for count in range (num):
Star (10)
( Num: INTEGER)
OUTPUT “*”
Star (10)
print( “*” )
NEXT Counter
10
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
Code
RETURN value
ENDFUNCTION
def identifier (Parameter) :
Code
return value
Function
Write a function that convert a centimeter to meter
Pseudocode
Function Meter
ENDFUNCTION
Mvalue  Meter (10)
Python
def Meter (cm)
ans = cm/100
mvalue = Meter (10)
( cm : INTEGER) RETURN REAL
return ans
RETURN
Ans  cm / 100
Ans
Function
FUNCTION Meter ( Cm : INTEGER ) RETURN REAL
Ans  Cm / 100
RETRUN Ans
Mvalue  Meter (10)
10 0.1
Exercise
1.Write a procedure that convert the given temperature from Fahrenheit to
Celsius and print result temperature
2. Write a function that convert the given temperature from Celsius to
Fahrenheit .
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
Library SubProgram
 Pseudocode
MOD – return remainder of a division
DIV – return the whole number part of division
ROUND – returns the value of rounded to a given number of decimal places
RANDOM – return random number between 0 and 1
Res  MOD (5 , 2)
Res  DIV ( 5 , 2)
Res  ROUND ( 5.12344 ,2) // 5.12
Res  RANDOM( )
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
DECLARE Number3, Answer : INTEGER
Number1 ← 10
Number2 ← 20
Number3 ← 30
Answer ← Number1 + Number2
OUTPUT "Number1 is now ", Number1
OUTPUT "Number2 is now ", Number2
OUTPUT "Answer is now ", Answer
ENDPROCEDURE
Number1 ← 50
Number2 ← 100
Answer ← Number1 + Number2
OUTPUT "Number1 is ", Number1
OUTPUT "Number2 is ", Number2
OUTPUT "Answer is ", Answer
CALL Test
OUTPUT "Number1 is still ", Number1
OUTPUT "Number2 is still ", Number2
OUTPUT "Answer is still ", Answer
OUTPUT "Number3 is ", Number3
Number1 = 10
Number2 = 20
Answer = 150
Answer = 30 // local
Number3 = 30// local
DECLARE a,b, c : INTEGER
PROCEDUR Example ()
BEGIN PROCEDURE
DECLARE a , b , ans: INTEGER
a 10
b 30
c a + b
ans c
OUTPUT a , b, c// 10 ,30 . 40
END PROCEDURE
a 100
b 200
c a + b
OUTPUT a , b , c // 100,200,300
Eample ()
OUTPUT a, b, c // 100 ,200 ,40
a , b, c //global var
a = 100
b =200
c = 40
a, b, ans // local var
a = 10 //local
b= 30
ans = 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
Bubble sort (Ascending Order)
1. Start at the beginning of the list
2. Compare the values in the position 0 and
position 1 in the list – if they are not in
ascending order then swap them
3. Compare the values in the position 1 and
position 2 in the list and swap if
necessary
4. Continue to the end of the list
5. If there have been any swaps repeat step
1 to 4
Sorting Algorithm
4 2 6 1 3
4
2 6 1 3
6 1
4
2 1 6 3
3 6
Pass 1
4
2 1 3
4
2 1
Sort 4 2 6 1 3 in ascending order
using bubble sort
Last = 4
4
2 6
1 3
4
2 6
1 3
4
2 6
1 3
3
2 6
1 4
4
2 6
1 3
4
2 6
1 3
4
2 6
1 3
4
2 6
1 3
Sorting Algorithm
Pass 2
Pass 3

More Related Content

PPTX
Presentation of computer
PPTX
9. DBMS Experiment Laboratory PresentationPPT
PDF
modularization-160202092213 (1).pdf
PPTX
SAP Modularization techniques
PDF
Modular programming in qbasic
PPSX
Algorithms, Structure Charts, Corrective and adaptive.ppsx
PDF
As Level Computer Science Book -2
PPTX
Modular programming
Presentation of computer
9. DBMS Experiment Laboratory PresentationPPT
modularization-160202092213 (1).pdf
SAP Modularization techniques
Modular programming in qbasic
Algorithms, Structure Charts, Corrective and adaptive.ppsx
As Level Computer Science Book -2
Modular programming

Similar to Subroutines igcses computer science powerpoint (20)

PPTX
Modularisation techniques new
PPTX
Subprogramms
PPTX
10.-Modular-Programming-with function and Sub Procedure.pptx
DOC
Lab 9 sem ii_12_13
PDF
Function procedure c6 c7
PPTX
Chap6 procedures & macros
DOC
Chapter 6 notes
DOC
Chapter 5 notes new
PDF
PPT
15 functions
PDF
Abap basics 01
PPT
Functions and pointers_unit_4
PPTX
Modular programming
PPT
SD & D modularity
PDF
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
PPTX
Functions and procedures
PDF
Unit iii vb_study_materials
PPT
16 subroutine
PPTX
Using general sub procedures
Modularisation techniques new
Subprogramms
10.-Modular-Programming-with function and Sub Procedure.pptx
Lab 9 sem ii_12_13
Function procedure c6 c7
Chap6 procedures & macros
Chapter 6 notes
Chapter 5 notes new
15 functions
Abap basics 01
Functions and pointers_unit_4
Modular programming
SD & D modularity
ceng2404314334535365_hgf66353week_07-08_v0.8.pdf
Functions and procedures
Unit iii vb_study_materials
16 subroutine
Using general sub procedures
Ad

Recently uploaded (20)

PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
Mega Projects Data Mega Projects Data
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
IB Computer Science - Internal Assessment.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Database Infoormation System (DBIS).pptx
PDF
Lecture1 pattern recognition............
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
Supervised vs unsupervised machine learning algorithms
Data_Analytics_and_PowerBI_Presentation.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
climate analysis of Dhaka ,Banglades.pptx
Reliability_Chapter_ presentation 1221.5784
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Mega Projects Data Mega Projects Data
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
IB Computer Science - Internal Assessment.pptx
Miokarditis (Inflamasi pada Otot Jantung)
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Database Infoormation System (DBIS).pptx
Lecture1 pattern recognition............
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
Clinical guidelines as a resource for EBP(1).pdf
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Supervised vs unsupervised machine learning algorithms
Ad

Subroutines igcses computer science powerpoint

  • 2. 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
  • 3. 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) Code ENDPROCEDURE def identifier (Parameter) : Code Parameter :the names of the variable that are used in the subprogram to the data passed from the main program as argument.
  • 4. Procedure without parameter PROCEDURE Star OUTPUT “*****” ENDPROCEDURE CALL STAR Main Program Write a procedure that print five “*” . Pseudocode PROCEDURE Star OUTPUT “*****” ENDPROCEDURE CALL Star SubProgram (Proecedure) Python def Star () print( “*****” ) Star ()
  • 5. Procedure without parameter PROCEDURE Star (Num : INTEGER) FOR Count  1 TO Num OUTPUT “*” NEXT Counter ENDPROCEDURE CALL STAR (10) Main Program Write a procedure that take the number as parameter and print the given number of * Pseudocode PROCEDURE Star FOR Count  1 TO Num ENDPROCEDURE CALL SubProgram (Precedure) Python def Star (num) for count in range (num): Star (10) ( Num: INTEGER) OUTPUT “*” Star (10) print( “*” ) NEXT Counter 10
  • 6. 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 Code RETURN value ENDFUNCTION def identifier (Parameter) : Code return value
  • 7. Function Write a function that convert a centimeter to meter Pseudocode Function Meter ENDFUNCTION Mvalue  Meter (10) Python def Meter (cm) ans = cm/100 mvalue = Meter (10) ( cm : INTEGER) RETURN REAL return ans RETURN Ans  cm / 100 Ans
  • 8. Function FUNCTION Meter ( Cm : INTEGER ) RETURN REAL Ans  Cm / 100 RETRUN Ans Mvalue  Meter (10) 10 0.1
  • 9. Exercise 1.Write a procedure that convert the given temperature from Fahrenheit to Celsius and print result temperature 2. Write a function that convert the given temperature from Celsius to Fahrenheit .
  • 10. 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
  • 11. Library SubProgram  Pseudocode MOD – return remainder of a division DIV – return the whole number part of division ROUND – returns the value of rounded to a given number of decimal places RANDOM – return random number between 0 and 1 Res  MOD (5 , 2) Res  DIV ( 5 , 2) Res  ROUND ( 5.12344 ,2) // 5.12 Res  RANDOM( )
  • 12. 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
  • 13. 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
  • 14. Example DECLARE Number1, Number2, Answer : INTEGER PROCEDURE Test DECLARE Number3, Answer : INTEGER Number1 ← 10 Number2 ← 20 Number3 ← 30 Answer ← Number1 + Number2 OUTPUT "Number1 is now ", Number1 OUTPUT "Number2 is now ", Number2 OUTPUT "Answer is now ", Answer ENDPROCEDURE Number1 ← 50 Number2 ← 100 Answer ← Number1 + Number2 OUTPUT "Number1 is ", Number1 OUTPUT "Number2 is ", Number2 OUTPUT "Answer is ", Answer CALL Test OUTPUT "Number1 is still ", Number1 OUTPUT "Number2 is still ", Number2 OUTPUT "Answer is still ", Answer OUTPUT "Number3 is ", Number3 Number1 = 10 Number2 = 20 Answer = 150 Answer = 30 // local Number3 = 30// local
  • 15. DECLARE a,b, c : INTEGER PROCEDUR Example () BEGIN PROCEDURE DECLARE a , b , ans: INTEGER a 10 b 30 c a + b ans c OUTPUT a , b, c// 10 ,30 . 40 END PROCEDURE a 100 b 200 c a + b OUTPUT a , b , c // 100,200,300 Eample () OUTPUT a, b, c // 100 ,200 ,40 a , b, c //global var a = 100 b =200 c = 40 a, b, ans // local var a = 10 //local b= 30 ans = 40
  • 16. 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]
  • 17. 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
  • 18. Bubble sort (Ascending Order) 1. Start at the beginning of the list 2. Compare the values in the position 0 and position 1 in the list – if they are not in ascending order then swap them 3. Compare the values in the position 1 and position 2 in the list and swap if necessary 4. Continue to the end of the list 5. If there have been any swaps repeat step 1 to 4 Sorting Algorithm 4 2 6 1 3 4 2 6 1 3 6 1 4 2 1 6 3 3 6 Pass 1 4 2 1 3 4 2 1 Sort 4 2 6 1 3 in ascending order using bubble sort Last = 4
  • 19. 4 2 6 1 3 4 2 6 1 3 4 2 6 1 3 3 2 6 1 4 4 2 6 1 3 4 2 6 1 3 4 2 6 1 3 4 2 6 1 3 Sorting Algorithm Pass 2 Pass 3