3.1 Introductions to modules
Module is group of statements that exist within a program for the purpose of performing a specific task. This allows for a big task to be broken up into several smaller tasks
Benefits of using modules
- Code is simpler
- Coe can be reused easier
- Each module can be tested alone to make sure it works before added to whole
- Faster development time
- Easier to facilitate teamwork
3.2 Defining and calling a module
Module names
Modules should have descriptive names. They will usually follow the rules for naming variables in your language of choice.
Defining and calling a module
Modules usually consist of a header, where it starts and is named, and a body, where the code is defined.
Calling a module
In the program there will be a line that runs the module. This is called calling the module. In reality all programs will have one module called main that will call the other modules.
Flowcharting a program with modules
A rectangle with lines down the sides represents a call to a module.
Top-down design
The main module is broken into smaller modules. These modules are examined to see if they can be broken up further. This continues till the simplest modules are designed and then coding is done.
Hierarchy charts
A series of boxes with lines connecting the modules that call other modules is a hierarchy chart.
3.3 local variables
A local variable is one that is declared in a module. It cannot be used outside the module.
Scope and local variables
Scope is the area in which a variable can be used. It will only work in the areas it was created in.
Duplicate variable names
A module can only use a name once. If an attempt to declare it again it will error. However the name can be declared in another module.
3.4 Passing arguments to modules
When it is necessary for one module to use information from another we can pass one or more variables. These are called parameters. The module will define what type of variable will be accepted.
Argument and parameter compatibility
When you pass a parameter the variables must be of the same type.
Parameter variable scope
The scope of a passed variable is the module that called it.
Passing multiple arguments
A list can be passed to a module. The module will receive them in the order sent.
Passing arguments by value and by reference
Passing arguments by value
Everything looked at so far passes variables by making a copy of them and pushing the value of it to the copy. The original is left alone.
Passing arguments by reference
Passing by reference allows for the variable to be changed. A copy is not made but a pointer to the value is made.
3.5 Global variables and global constants
Global variables
Another way to be able to modify a variable is to declare it in such a way as to be available to every variable. This is called a global variable and in most languages it is declared early in the program. Using global variables can be tricky.
- Any module can change it making debugging tricky
- It makes reusing those modules tricky as you have to remember to declare the global variable again
- It makes the program harder to understand.
Global constants
Global constants are OK because they cannot change. Use them where you have a value that will not change.
No comments:
Post a Comment