1 / 16

Introducing Modularity

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.

klohmann
Download Presentation

Introducing Modularity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introducing Modularity

  2. 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.

  3. 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.

  4. 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 …

  5. Data Flow Between Modules

  6. 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)

  7. 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.

  8. 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.

  9. lvalue & rvalue • 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.

  10. 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.

  11. 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.

  12. 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

  13. 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.

  14. 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 FloatSet Num = Cube(10)Write Num End Main Function Cube(intX) as FloatSet Cube = intX^3 End Cube

  15. Questions?

  16. Resources • Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.

More Related