0% found this document useful (0 votes)
19 views20 pages

Block Structure

Uploaded by

tc9466
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

Block Structure

Uploaded by

tc9466
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Block Structure

Block structured program is a set of blocks.

A block is section of code having its own local variables.

Beginning of block consists of set of declarations for variables


which are stack dynamic means storage is allocated when block is
entered and deallocated when it is exited.

Variable declarations are followed by a set of statements in which


those variables are referenced.

The block-structure paradigm is characterized by:


1.Nested blocks.
2.Procedures
3.Recursion

Nested blocks: It allow the definition of one block contain the


definition of other block.

At the outermost level, a program consists of a single block(Main


program) which contains the other blocks callable from the main
program.
Overview of Management
Procedures:

Procedures are the named blocks that can be called


from other parts of a program, and it facilitates
information exchange between calling and called
blocks through parameters.

Recursion:

The implementation model for blocks is the stacks.

Successive invocations of a recursive procedure can be


pushed onto the run time stack and popped in reverse
order, passing values back down the stack.

When a block terminates, its memory allocation will be


popped, and memory for calling block reactivated.

Overview of Management
Static-scope rules for block structured languages:

The declaration at the beginning of block define the local referencing


environment for the block.

If for an identifier referenced there is no local declaration then


reference is checked in the block that immediately encloses that
block and so on.

Finally, if no declaration is found then the reference is taken as an


error.

If a block contain another block definition , then any local declaration


in the inner block are completely hidden from the outer block and
cannot be referenced from it.

The block name become the part of local referencing environment of


the containing block.

Overview of Management
Local data and local referencing environment:

Local data is defined inside the subprogram and access to this


data is restricted to subprogram in which they are defined.

The local variables are stack dynamic, they are bound to


storage when the subprogram begins execution and unbound
from storage when that execution terminates.

Stack dynamic variables provides following advantages:

1.It provides subprogram flexibility.

2.It is essential for recursive subprograms.

3.Some of the storage for local variables of all subprograms can


be shared.

Overview of Management
Scope rule for local referencing environment:

Static scope rule:

It is implemented by a table which contain an identifier and


associated data object.

The name is used so that later references to that variable will be


able to determine where that variable will resides in memory
during execution.

Dynamic scope rule:

The reference to an identifier during the execution of


subprogram refers to the association for that identifier in the
current activation of the subprogram.

There are two methods for dynamic scope:

1. Retention
2. Deletion
Overview of Management
Retention

The identifier association and bound values are retained


after execution.

It is implemented by local environment tables which is


kept as a part of code segment.

The code segment is allocated storage statically and


remain in existence throughout execution.

The values stored at the end of one subprogram call will


be there when next call begins.

No special action is required to change from one local


environment to another as one subprogram calls another.

This approach is used by COBOL and many versions of


FORTRAN.

Overview of Management
Deletion:

The identifier association are deleted.

The table is kept as a part of activation record and is


destroyed after each execution.

The activation record is created on a central stack on


entry to subprogram, and deleted from the stack on exit.

This approach is used by C, APL and ADA Languages.

Overview of Management
Advantages and Disadvantages:

1.The retention approach is history sensitive.

2. The retention approach consume more storage space.

3. The deletion approach is more natural strategy for recursive


subprograms.

4. The deletion approach provide a saving in storage space.

5. The deletion approach does not allow any local data to be


carried over from one call to the next, so a variable that must
be retained between the calls must be declared as non-
local to the subprogram.

Overview of Management
Shared data

Data objects are generally shared among several subprograms.

There are two ways to gain access to shared data objects.

1.Direct sharing through parameter transmission.

2.Through direct access to non-local referencing environment.

Non-local variables are visible within the subprogram but are


not locally declared.

Global variables are visible in all program units. It forms the


part of non-local referencing environment.

Non-local referencing environment use four basic


approach :

1.Explicit common environment


2.Dynamic scope
3.Static scope
4.Inheritence
Overview of Management
Explicit common environment

A set of data objects to be shared among a set of subprograms is


allocated storage in a separate named block is called
common environment.

It is implemented as a separate block of memory storage by


using special keywords

The data objects within the block are visible within the
subprograms and may be referenced by name in usual way.

It is similar to local environment but it is not a part of any


single subprogram.

It contain definition of variables, constants and types.

It doesn’t contain subprograms and formal parameters.

Overview of Management
Inheritance:

Inheritance is the receiving of the properties or


characteristics of one component by other component
according to a special relationship that exists between the two
components.

We use it in programming language design

Parameter and parameter passing scheme:

It is more flexible than direct access to non-local variables.

There are two types of parameters:

1.Formal parameters

2. Actual parameters

Overview of Management
Establishing the correspondence between the parameters

1.Positional correspondence:

The actual and formal parameters are paired based on their respective positions
in actual and formal parameter lists.

The first actual parameter is bound to the first formal parameter and so forth.

2.Correspondance by explicit name:

When the parameter list is long then programmer can make mistakes in the order
of parameters in the list, for it we use keyword parameter.

In it name of formal parameter to which actual parameter is to be bound is


specified with the actual parameter explicitly.

For example in Ada sum(x-->a, y-->b), It pairs formal parameter x with actual
parameter a and y with b.

The disadvantage of keyword parameter is that user of subprogram must


know the names of formal parameters.
Overview of Management
Parameters passing methods:

It refer to the matching of actual parameters with formal


parameters when a subprogram calls occurs.

It is the way in which the parameters are transmitted to and from


the subprogram.

There are two approaches :

1.Semantic models of parameter passing.

2.Implementation models of parameters passing

Overview of Management
Semantic models of parameter passing
There are three methods:

IN mode: In it values are passed to the subprogram being called i.e. formal
parameters can receive data from the corresponding actual parameters.

Inside the subprogram , an IN parameters acts like a constant.

OUT mode: It returns values to the caller of the subprogram i.e. formal
parameters can transmit data to the actual parameter.

Inside the subprogram, an OUT parameter acts like a variable.

OUT formal parameter act as a local variable and its value can be changed in any
way.

IN OUT mode: In it initial values are passed to the subprogram being called and
return updated values to the caller.

Inside the subprogram, IN OUT parameter acts like an initialized variable.

The actual parameters that correspondence to an IN Out formal parameters must


be a variable. Overview of Management
Implementation models of parameter passing

The various methods for transmitting parameters are:

1.Call by value
2.Call by reference
3.Call by result
4.Call by name
5.Call by value-result
6.Call by constant value

Overview of Management
Call by result

It is an implementation model for OUT mode parameter.

It is used only to transmit a result back from a subprogram.

When the subprogram terminates, the final value of formal parameters is


assigned as the new value of the actual parameter.

Disadvantages:

1.It require extra storage.

2.There can be an actual parameter collision.

3. The portability problem that are difficult to diagnose.

Overview of Management
Call by value result

It is an implementation model for IN OUT mode parameter in which


actual values are moved.

The value of the actual parameter is used to initialize the corresponding


formal parameter which then act as a local variable.

In it formal parameters must have local storage associated with called


subprogram.

When the subprogram terminate, the value of formal parameter is


transmitted back to the actual parameter.

Disadvantages:

1.Multiple storage for parameters

2.Time for copying values

3. The order in which actual parameters are assigned.


Overview of Management
Call by name

In it, actual parameter is textually substituted for the corresponding formal


parameter in all its occurrences in the subprogram.

It is bound to an access method at the time of subprogram call, but actual


binding is delayed until the formal parameter is assigned or referenced.

Disadvantages:

1.The process is slow relative to other parameter passing methods.

2. High cost in execution efficiency.

3. Difficult to implement.

4. Some operations are not possible with call-by-name parameters

Overview of Management
Call by constant

In it, no change in value of formal parameter is allowed during program


execution.

No assignment of a new value or other modification of the value of parameter


is allowed.

The formal parameter may not be transmitted to another subprogram expect


as a constant value parameter.

The formal parameter act as a local constant during execution of


subprogram.

Advantages:
1.Protecting the calling program from changes in actual parameter

2. The value of actual parameters cannot be modified by the subprogram.

Overview of Management

You might also like