1 / 18

Data Flow Diagrams and Parameters in Program Modules

Learn about the concept of program modules, sharing of data using parameters, and the importance of passing arguments in correct order. Explore value and reference parameters and how they affect variables in the calling module. Discover how functions can be used as subprograms within expressions.

huguley
Download Presentation

Data Flow Diagrams and Parameters in Program Modules

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. Extended Prelude to Programming Concepts & Design, 3/e byStewart Venit and Elizabeth Drake Chapter 7:More About Program Modules and Subprograms

  2. 7.1 Data Flow Diagrams and Parameters • The concept of program modules was introduced in Chapter 2. • Program modules do not have access to all of the variables from the other modules. Some data will be required to be shared between modules. • Sharing of data is done by passing parameters from one module to another. Passing data to a module is known as exporting data. Passing data into a module is known as importing data. • The sharing of data can be described using data flow diagrams.

  3. Data Flow Diagram using the Sale Price Computation Problem from the text

  4. Parameters and Arguments • Suppose we have designed a module whose purpose is to output the results of some calculations. We need to pass the results of the calculations (the data) to that module so that it can display the output. The syntax for defining a module that accepts data (has parameters) is shown below. Also shown is how to call such a module and how to pass in the data (arguments). • Notice that the names of the variables that are used in the calling module do not need to be the same as the names in the called module.

  5. Parameters and Arguments (continued) • When a subprogram is called, the values of the arguments are assigned to corresponding parameters, based on the order of appearance in the two lists. • Therefore, it is very important to pass values in correct order!

  6. Call Exam(9, 3) Subprogram Exam (time, room) Write “Your History Final” Write “will be at” Write time, “o’clock in” Write “Room Number “, room End Subprogram Display: Your History Final will be at 9 o’clock in Room Number 3 Call Exam(3, 9) Subprogram Exam (time, room) Write “Your History Final” Write “will be at” Write time, “o’clock in” Write “Room Number “, room End Subprogram Display: Your History Final will be at 3 o’clock in Room Number 9 Parameters and Arguments (continued) The Subprogram Exam displays the time and place of your History Final. The Final is at 9:00 o’clock in Room Number 3. See what could happen if the arguments are not passed in correct order!

  7. Why Use Arguments and Parameters? • Modules are more valuable since they can be written in a more general fashion and be used over and over. • Modules are easier to design and write. They are defined in terms of what data they need from outside and 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. 7.2 More About Subprograms • Parameters can be passed by value and by reference. • Data may be sent from the caller (main program) to the subprogram. This data is passed or imported to a subprogram. • Data may also be passed from the subprogram back to the caller. This data is returned or exported to the main program. • The correspondence between arguments and parameters does not indicate which way the data is flowing.

  9. Value and Reference Parameters • Value parameters have the property that changes to their values in the subprogram do not affect the value of the corresponding (argument) variables in the calling module. These parameters can only be used to import data into a subprogram. • Reference parameters (also called variable parameters) have the property that changes in their values do affect the corresponding arguments in the calling module. They can be used to both import data into and export data from a subprogram.

  10. Value and Reference Parameters Pass by value: When a variable is passed by value to a submodule, the submodule only receives a copy of that variable. A separate storage location is created and the value of the variable is stored in that location. Therefore, any changes made to that variable in the subprogram do not affect the original variable. Pass by reference: When a variable is passed by reference, the submodule receives the actual storage location of that variable. Therefore, changes made to the variable in the subprogram are also made to the original variable.

  11. Example: Pass by Val and Pass by Ref 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 Num1 is passed by value, and it is not changed by the subprogram. Num2 is passed by reference and it is changed by the subprogram. This program prints: 1 1

  12. 7.3 Functions • A function is a subprogram whose name can be assigned a value. This allows the calling program to use the name of the function in an expression. Some examples were already introduced in earlier chapters: Sqrt(X) Int(X) Length(S) EOF(FileName) Example: Set X = Sqrt(10*47)+ 101 The function call Sqrt(10*47) evaluates to a numeric value which is then used in the expression. After this statement is executed, the value of X is 571.

  13. Built-in Functions Most programming languages provide built-in functions. The code for these functions are supplied in separate modules and can be used by any programmer. Some examples of built-in functions are: • Abs(X) computes and returns the absolute value. It is of type Real. • Round(X) rounds the real number, X, to the nearest whole number. It is of type Integer. • Str(X) converts the number X to a corresponding string. It is of type String.

  14. User-Defined Functions User-defined functions are created by the programmer. The differences between a function and a subprogram are: • A function’s name may be assigned a value in the code that defines it. • A function is called by placing its name with any arguments anywhere in the program where a constant of the function’s type is allowed.

  15. Defining Functions • Since function calls evaluate to some value, the definition of the Function must include which data type the function evaluates to. • The following pseudocode shows the syntax for defining a Function that finds the cube of a number: Main Program Declare Num As Real Set Num = Cube(10) Write Num End Program Function Cube(X) As Real Set Cube = X^3 End Function

  16. 7.4 Recursion • A recursive subprogram is one that calls itself. • Some languages do not permit recursion. • Recursive subprograms may be an effective way of providing a solution for a specific problem. • Problems that are solved with recursion are those that can be easily described in terms of themselves, for example, the sum of the first N integers can be written as: Sum(N) = Sum(N – 1) + N

  17. Recursive Code Example Function Sum(N) As Integer If N = 1 Then Set Sum = 1 Else Set Sum = Sum(N - 1) + N End If End Function • Tracing recursive code is difficult at first. The recursive call must be conditional, or there will be an infinite recursion. This is similar to a looping construct, but no repetition is used; there are only function calls to the function we are defining. Recursive call to Sum

  18. Pseudocode Language (Ch 7) In this chapter we added some more syntax to our language. We discussed how to pass parameters by value and by reference. We also we saw how to define functions. Defining Subprograms with Parameters Subprogram Name (Param1 as Ref, Param2) End Subprogram Defining Functions FunctionName (Param1, Param2) As DataType End Function

More Related