sd & d modularity

19
Modularity

Upload: forrester-high-school

Post on 18-Aug-2015

45 views

Category:

Education


5 download

TRANSCRIPT

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?

Modularity

Computer programs are much easier to program and understand if we break them down into smaller blocks, or sub programs.

Larger programs will have teams of developers working on them so it makes for each developer to work on their own sub-program.

Modularity means breaking large complex programs into smaller and more manageable sub-programs.

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.

Modularity

We create a sub-program (or subroutine) to handle the movement of the ghost. We tell the sub-program 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 IFEND SUBROUTINE

This sub-program can be used for any ghost.

Sub-programs (subroutines)

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”)

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

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.

Parameters

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

Procedures and functions

The Pacman ghost subroutine move_ghost is an example of a procedure.

It creates an effect by changing the direction of the ghosts.

Procedures

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.

Functions

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.

Parameter passing by value

A parameter is passed by reference if its value is changed by the subroutine.

increaseAge(age)

PROCEDURE increaseAge(age) age = age + 1END 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.

Parameter passing by reference

Example - CIs this a function or a procedure?

What does it do?

What are the parameters?

Example - C

Example - JavaScriptIs this a function or a procedure?

What does it do?

What are the parameters?

Example - JavaScript

Example - JavaScriptIs this a function or a procedure?

What does it do?

What are the parameters?

Module LibrariesOften a module of program code written for one project

can be reused in another project

Through time, companies will save modules for reuse in other projects in a module library

This can save time and therefore money

Modules will already have been tested so the developer should be confident that the code will work