Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Programming Local Identifiers, Functions, Modularity and Data Flow.

Similar presentations


Presentation on theme: "Pascal Programming Local Identifiers, Functions, Modularity and Data Flow."— Presentation transcript:

1 Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

2 Pascal Programming n Problem: In our procedure we want to exchange black for white. A simple exchange will not work. n So, we need Temporary for storage. n We may have used Temporary in other procedures. So, what do we do?

3 Pascal Programming n We declare Temporary within our procedure. n The declaration... Inside the procedure and before begin (Sound familiar?) n The declaration terminates when the procedure finishes its work.

4 Pascal Programming n e g: –procedure ChangeColor; {Changes one color for another} –var Temporary, Black, White : integer; –begin Temporary := Black Black := White White :=Temporary....

5 Pascal Programming n Variables that affect the entire program -- Global Variables n Variables that affect the procedure ONLY Local Variables n A variable of the same name may have two different uses. The local variable controls.

6 Pascal Programming n Local rule... Any declaration allowed in a Pascal program is allowed in a Pascal procedure. n Local identifiers make the procedure self-contained and self-sufficient. n A variable declared in a procedure... –Is local to that procedure –its meaning confined to the procedure –you need only know it when writing or using that procedure

7 Pascal Programming n Block...parameter list, declaration set and body of statements under their influence. n All identifiers in the block are said to be local to the block n Scope...the area of influence of an identifier--may be global or local.

8 Pascal Programming n Functions may be... –Predefined...sqrt, trunc and round –Programmer defined n A function call is an expression...not a statement like a procedure. n Functions return value(s). n Functions can act like procedures... But that will create unintended side-effects. Use a function for its intended purpose... creating and delivering value.

9 Pascal Programming n e g: n function Cube (X: integer): integer; n {returns cube of X} n begin {cube}; –Cube :=X*X*X; n end {cube};

10 Pascal Programming n The declaration... n Starts with the reserved word function. n Includes the type for the function in the heading. n In the body... n The function name must be on the left side of an assignment statement.

11 Pascal Programming n Data flow... n When we break a problem into component parts, we need to trace the data through each of the elements or sub-tasks. n We use a data flow diagram for this. n This structured diagram clarifies the role and relationship of each sub-task.

12 Pascal Programming Get Data Determine balance after down payment Determine monthly interest Determine length of loan Create amortization table

13 Pascal Programming n Data Flow Analysis... n will allow you to track the movement of data. n will help you decide whether a sub-task should be a function or a procedure. n will help a programmer test the soundness of the program.

14 Pascal Programming n Summary... n Local variables hold data within a procedure... Not outside of it. n Parameters should establish the interaction of a procedure with the larger program. n A formal value parameter is equal to the value held at the time of the call. Continue...

15 Pascal programming n When an actual variable parameter does not change a value when expected... Check the var in the parameter list. n When a sub-task requires computation of a value... Use a function. n Eliminate side-effects in functions by not making them into procedures. n Data flow analysis helps determine parameters and their type.


Download ppt "Pascal Programming Local Identifiers, Functions, Modularity and Data Flow."

Similar presentations


Ads by Google