PASCAL   vs  FORTRAN

Handling Of Sequence Control and handling of Subprograms and Storage

 

Introduction and definitions:

Sequence control is the control of the order of execution of a program. Sequence control is either implicit or explicit. Sequence control can be categorized into three groups:

            1. Expressions- viz: Precedence rules and parenthesis

            2. Statements- viz: for, if, switch,etc.

            3. Subprograms- viz: Subprogram calls and coroutines.

 

Impilicit sequence control structures are defined by the language. They are also called default structures.

Explicit sequence control structures are those a programmer may use to modify implicit sequence control structures. e.g. parenthesis, go to, and labels.

 

Pascal and FORTRAN are both block structured languages. Blocks are sections of a program that allow local variables, whose scopes are limited, to be defined. Such variables are stack dynamic, so they have their storage allocated when the section is entered and deallocated when the section is exited.

 

Subprogram:

Subprograms are the fundamental building blocks of programs. There are two categories of subprograms:

1. Procedures - procedures are  collections of statements that define parametized                        computations. Procedures thus define new statements that may serve in place of one not available in a particular language.

2. Functions define new user defined operators. They produce no side effects, that is they neither modify their parameters nor any variables defined outside them.

 

 

PASCAL  and  FORTRAN

 COMPARED and CONTRASTED

Handling of Sequence Control

 

Pascal and FORTRAN are both procedural languages. Procedural languages are executed in a step by step manner. We compare and contrast both languages using the following subtopics:

1. Handling of Sequence control

2. Handling of Subprograms and Storage Management.

 

Handling of sequence control within expressions:

(a) Precedence Rules

Expressions are means of specifying computations in a program. The order of  operation of operands and operators are crucial to understanding sequence control. The flow of sequence control within expressions is governed by operator precedence rules and associativity.

Pascal and Fortran are both imperative languages, so their operator precedence rules are essentially the same. They are based on those of mathematics. Exponentiation has the highest precedence (if provided), followed by multiplication and division on the same level. All + and - have equal precedence. Pascal, however, does not provide exponentiation as does Fortran.

 

(b) Associtivity

Associativity rules come into play when two operators with the same level of precedence occur next to one anothere in an expression, and a decision must be made as to which one is to be evaluated first. The decision to evaluate any particular operator first is made using the associativity rules of the language.

Associativity in Pascal and Fortran are left to right. Fortran, however, provides exponentiation which associates right to left.  

 

(c) Parentheses

Parentheses are  very powerful in expressions. Parentheses used in arithmetic expressions could modify or get rid of precedence rules altogether. both Pascal and Fortran permit the use of parentheses in expressions.

 

Handling of Sequence Control among statements.

Another important level of sequence control is the control of flow among statements. Selection statements and iterative statements are examples of control statements. A selection statement provides the means of choosing between two or more execution paths in a program. Selection statements can be two-way, n-way, or multi-way.

 

Selectors could be single-way, like if-statements. Pascal and Fortran provide for  

single-way as well as two-way selectors which includes if-else statements. Fortran’s arithmetic IF is a 3-way selector, compared to a logical IF statement. Some earlier versions of Fortran, like Fortran IV did not include a 2-way selector.

 

Selectors could also be multiple-way. A versatile multiple-way selector statement is the ‘case’ statement. Pascal and Fortran both provide for ‘case’ statements. Pascal’s ‘case’ is of the form

case  expression of

      begin

      constant_list_1: statement_1;

       .....

      constant_list_n: statement_n

      end

The expression is evaluated and its value is compared with the constant in the constant list. If a match is found, control is transferred to the statement attached to it.

Fortrans CASE statements compares closely with Pascal’s. However, there are some differences.

In Fortran,s list of constants items are seperated by the OR operator. Another feature of fortran is the GO TO statement that performs multiple selection of opereations.

 

Another form of control among statements is Iterative statements. An iterative statement is one that causes a statement or collection of statements to be executed zero, one, or more times.

In FORTRAN, the DO statement performs iteration. FORTRAN 90  DO statement is a post test statement, in contrast to PASCAL’S which is a pre-test statement.

 

Another form of control among statements is logicall controlled loops. Pascal provides a post-test logical  loop statement, repeat-until. FORTRAN 90 on the other hand has neither a pre-test or a post-test logical loop.

 

Handling Of Subprograms

Subprograms, as described earlier, are the building blocks of  programs. A subprogram is a collection of  statements which form a section of a program. Subprograms are the foundations of code reuse hwich saves memory space and coding time. instead of giving the details of how some computation is to be done each time it comes up in a program, such a collection of statements is abstracted away in a subprogram and called when needed. Abstracting away details in a subprogram increases program readability.  

 

There are two kinds of subprograms:  Procedures and Functions. A procedure is a collection of statements that defines parameterized computations. Procedures are capable of changing variables in both the procedure and the calling program unit. Functions on the other hand do not modify their parameters or the variables defined outside the functions.

 

Both Pascal and FORTRAN support subprograms. Local variables are an important component of subprograms. Each subprogram can define its own local variables. Pascal’s local variables are stack-dynamic.  FORTRAN supports both static and stack-dynamic local variables.

 

Parameter passing is another component of subprograms. Parameters are always passed to and from subprograms during subprogram calls. FORTRAN uses both pass-by-reference and pass-by-value-result methods of parameter passing, while Pascal uses pass-by-value as default and pass-by-reference can be specified.

Subprograms are also passed as parameters to other subprograms. This is supported by Pascal and FORTRAN.

 

Seperate Compilation and Independent Compilation of Subprograms

Subprograms, in many ways, ease program writing. Large software systems depend on the capability of programming languages to compile a long program in parts without compiling the whole program. Seperate compilation means that compilation units can be compiled at different times, but their compilations are not independent of each other. Independent compilation means that program units can be compiled without information about any other program units in the program.

The original version of Pascal and FORTRAN II did not support seperate or independent compilations. Later versions of Pascal and FORTRAN support seperate and independent compilations.

 

Handling Storage Storage Management 

Programming languages often contain features or restrictions whose only reason for inclusion is

to allow a particular sotrage management technique to be used, e.g., FORTRAN's restricts

subprogram calls to be nonrecursive. This allows subprograms to be implemented without

needing a run-time stack for return points. Language implementors must choose between

different techniques to implement storage management requirements. Storage management

encompasses storage of data and translated programs, of code segments for translated user

programs, of system run-time programs (e.g., library routines), of user-defined data structures

and constants, of subprogram return points, of referencing environments, of temporary storage

in expression evaluation, of temporary storage in parameter transmission, of input-output

buffers, and of other miscellaneous system data. Storage management must also take care of

the allocation and de-allocation of storage for subprogram call and return operations, for data

structure creation and destructio noperations, and of component insertion and deletion

operations. These operations require explicit storage management. Other operations require

some hidden storage management and generally involves allocation and de-allocation of

temporary storage for housekeeping functions which may occur during expression evaluation.

 

Storage management has basically three phases:

 

   1.Initial allocation

   2.Recovery (garbage collection)

   3.Compaction and reuse

 

Most storage management techniques are some variation of one of these three basic phases.

Storage management is either static allocation or dynamic allocation. Static allocation is the

simplest with allocation during translation remaining fixed throughout execution; it requires no

run-time storage management software and no mechanisms for recovery or reuse. Storage for

code segments for user and system programs, for I/O buffers and for other system data is

generally done statically. While static allocation is more efficient (requires no time or space

during execution), it is incompatible with recursive subprogram calls, with variable data

structures, and a number of other desirable language features.

 

Run-time storage management in its simplest form involves a stack where free storage is set up

at the start of execution as a sequential block in memory. A stack pointer is all that is required

to control storage management. The stack pointer always points to the top of the stack where

the next available block of free storage is located. Compaction occurs automatically as part of

freeing storage.

 

Often storage is tied to subprogram activities, so the last in-first out structure of subprogram

calls and returns works quite well with a stack management structure. A single activation

record is used to group elements associated with a subprogram activation. A new activation

record is created at the top of the stack when a subprogram call is made, and it is deleted

upon termination.

 

Heaps are also used for storage management and handle both fixed-size and variable-size

elements. Heaps allocate and de-allocate storage in some relatively unstructured manner

causing severe problems wiht storage allocation, recovery, compaction and reuse.

 

 

REFERENCES:

1. Programming Languages by Robert W. Sebesta

2. CS 441 Notes by Mary Lou A. Hines