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

Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Embed Size (px)

Citation preview

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

Pascal Programming

Local Identifiers, Functions, Modularity and Data Flow

Page 2: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Problem: In our procedure we want to exchange black for white. A simple exchange will not work.

So, we need Temporary for storage. We may have used Temporary in other

procedures. So, what do we do?

Page 3: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

We declare Temporary within our procedure.

The declaration . . . Inside the procedure and before begin (Sound familiar?)

The declaration terminates when the procedure finishes its work.

Page 4: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

e g:– procedure ChangeColor;

• {Changes one color for another}

– var• Temporary, Black, White : integer;

– begin• Temporary := Black• Black := White• White :=Temporary . . . .

Page 5: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Variables that affect the entire program -- Global Variables

Variables that affect the procedure ONLY Local Variables

A variable of the same name may have two different uses. The local variable controls.

Page 6: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Local rule . . . Any declaration allowed in a Pascal program is allowed in a Pascal procedure.

Local identifiers make the procedure self-contained and self-sufficient.

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

Page 7: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Block . . .parameter list, declaration set and body of statements under their influence.

All identifiers in the block are said to be local to the block

Scope . . .the area of influence of an identifier--may be global or local.

Page 8: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Functions may be. . .– Predefined . . .sqrt, trunc and round– Programmer defined

A function call is an expression . . .not a statement like a procedure.

Functions return value(s). Functions can act like procedures . . . But that will

create unintended side-effects. Use a function for its intended purpose . . . creating and delivering value.

Page 9: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

e g: function Cube (X: integer): integer; {returns cube of X} begin {cube};

– Cube :=X*X*X; end {cube};

Page 10: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

The declaration . . . Starts with the reserved word function. Includes the type for the function in the

heading. In the body . . . The function name must be on the left

side of an assignment statement.

Page 11: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Data flow . . . When we break a problem into

component parts, we need to trace the data through each of the elements or sub-tasks.

We use a data flow diagram for this. This structured diagram clarifies the role

and relationship of each sub-task.

Page 12: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Get Data

Determine balance after down payment

Determine monthly interest

Determine length of loanCreate amortization table

Page 13: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Data Flow Analysis . . . will allow you to track the movement of

data. will help you decide whether a sub-task

should be a function or a procedure. will help a programmer test the

soundness of the program.

Page 14: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal Programming

Summary . . . Local variables hold data within a

procedure . . . Not outside of it. Parameters should establish the

interaction of a procedure with the larger program.

A formal value parameter is equal to the value held at the time of the call. Continue. . .

Page 15: Pascal Programming Local Identifiers, Functions, Modularity and Data Flow

Pascal programming

When an actual variable parameter does not change a value when expected . . . Check the var in the parameter list.

When a sub-task requires computation of a value . . . Use a function.

Eliminate side-effects in functions by not making them into procedures.

Data flow analysis helps determine parameters and their type.