Published on

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Modularity
  2. 2. Modularity A computer program could be written as one long sequential piece of code. But what happens if we want to carry out the same process several times throughout the program? We could copy and paste the code... ...but then what if we wanted to change the code?
  3. 3. Modularity Example: In the game Pacman, there are 4 ghosts. Each ghost changes to a new and random direction when it reaches a wall. The movement rules for each ghost are the same. It makes sense to develop one block of code which will work for each ghost.
  4. 4. Subroutines We create a subroutine to handle the movement of the ghost. We tell the subroutine which ghost is to be moved. SUBROUTINE move_ghost (ghost_name) IF ghost_name at edge wall THEN change ghost_name direction ELSE move ghost_name in current direction END IF END SUBROUTINE This subroutine can be used for any ghost.
  5. 5. Parameters Whenever we want to move a ghost, we call the subroutine: move_ghost (“Inky”) This calls the subroutine move_ghost, passing in the parameter (“Inky”)
  6. 6. Parameters Notice that the code to call the subroutine move_ghost: move_ghost (“Inky”) Passes the parameter “Inky”, and so the variable ghost_name will take on the value “Inky” SUBROUTINE move_ghost (ghost_name) IF ghost_name at edge wall THEN change ghost_name direction
  7. 7. Parameters The parameters which are passed into the subroutine when it is called from another part of the program are called the actual parameters. “Inky” is an actual parameter. The parameters which are used in the subroutine definition are called the formal parameters. ghost_name is a formal parameter.
  8. 8. Procedures and functions There are generally two kinds of subroutine, procedures and functions. Procedures and functions are a series of statements which have been given a name. The difference between a procedure and a function is that: A procedure produces an effect A function produces a value
  9. 9. Procedures The Pacman ghost subroutine move_ghost is an example of a procedure. It creates an effect by changing the direction of the ghosts.
  10. 10. Functions This subroutine is an example of a function: FUNCTION calc_area (length, breadth) area = length * breadth RETURN area END FUNCTION It produces a value, in this case area.
  11. 11. Parameter passing by value A parameter is passed by value if its value is not changed by the subroutine. pensionDetails = getPension (age) PROCEDURE getPension(person_age) IF person_age >65 THEN pension_due= TRUE END IF RETURN pension_due END PROCEDURE In this case, the parameter age is not changed by the subroutine.
  12. 12. Parameter passing by reference A parameter is passed by reference if its value is changed by the subroutine. increaseAge(age) PROCEDURE increaseAge(age) age = age + 1 END PROCEDURE In this case, the parameter age is changed by the subroutine. Not all languages support parameter passing by reference. Arrays are always passed by reference.