Introducing Modularity
Goals By the end of this unit, you should understand … … why programmers use modularity. … what a sub-program does. … the difference between passing by value and passing by reference. … what a function does.
Defining Modularity Modularity is the process of taking larger programming tasks and breaking them into smaller, more manageable pieces. Sometimes, we call upon a module to perform a certain task without giving back a value to the procedure that called it. We call such modules sub-programs. When a module returns a value to its calling procedure, we call it a function. The main module is the central point in a program through which we call many of a program’s other modules.
Sharing Data Program modules do not have access to all of the variables from the other modules. However, sometimes we need to share data between modules. We share data by passing parameters from one module to another. Think of this as “exporting” data, and “importing” data. The following data flow diagram shows how modules share data …
Data Flow Between Modules
Parameters & Arguments Suppose we designed a module to output the results of some calculations. We will need to pass the data to that module so that it can do the output. Below is the syntax for defining a module that accepts data (has parameters) and how to call such a module passing the data (arguments). Notice that the names of the variables in the “calling” module do not need to be the same as the names in the “called” module. Call OutputResults(OrigPrice,DiscountRate,SalePrice) Sub OutputResults(OldPrice,Rate,NewPrice)
Why Use Arguments & Parameters? Modules become re-usable and fit more general purposes. Modules become easier to design and write. Programmers define modules in terms of what data they need from outside, what data they will produce for export. The programmer writing a module is not concerned with details outside of the module being developed. Module testing, or “unit testing” is an important mechanism to debug / maintain large programs.
How Should We Pass Values? When we transfer data between modules, we must also decide whether we should pass those values “by value” or “by reference.” This is a difficult topic to understand. Before going further, let’s take a step back and re-examine how memory stores variable values.
lvalue & rvalue lvalue rvalue 100000 34 100001 “Bob” 100010 true When a program tells memory to store data, memory creates a table as a directory of the stored data. In that table, memory stores two different values for each piece of data. It stores a location value, or lvalue. It also stores the actual value, called the read value, or rvalue. lvalue rvalue 100000 34 100001 “Bob” 100010 true 100011 47.89 100100 “peace”
Passing By Value Most of the of time, we want to protect our original values stored in variables. To prevent a called subprogram or function from manipulating those values, we send a copy of a variable’s rvalue to the parameter of the called procedure. We give this the name “passing arguments by value.” It is the default way to share data between modules in most programming languages.
Passing By Reference Sometimes, we want to give a called procedure the ability to manipulate a variable value. We can do this by passing the variable’s lvalue to the parameter of the called procedure. We give this the name “passing arguments by reference.” We should be very careful when sending by reference.
By Value/By Reference Example Main Program Set Num1 = 1 Set Num2 = 2 Call Switch (Num1, Num2) Write Num1, “ ”, Num2 End Program Subprogram Switch (Number1, Number2 As Ref) Set Number1 = 2 Set Number2 = 1 End Subprogram
Functions Functions are modules that return a value to a calling procedure. We classify functions into two groups: Built-In Functions are those that are inherent to a programming language. User-defined Functions are those modules that a programmer writes to return a value.
Writing a Function Since functions return a value, the definition of the function must include which data type of the return value. Sub Main() Declare Num as Float Set Num = Cube(10) Write Num End Main Function Cube(intX) as Float Set Cube = intX^3 End Cube
Questions?
Resources Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.