Advanced Fortran Topics Hands-On Training Advanced Fortran ...
Simple Procedural Languages: FORTRAN and C Lecture 9: Dolores Zage.
-
Upload
cecil-moody -
Category
Documents
-
view
226 -
download
2
Transcript of Simple Procedural Languages: FORTRAN and C Lecture 9: Dolores Zage.
Simple Procedural Languages: FORTRAN and C
Lecture 9:
Dolores Zage
Procedural Languages Are the closest thing to “typical” programming
language for demonstrating common language features.
Language consists of a series of procedures that execute when called
Each procedure consists of a sequence of statements
Each statement manipulates data that either is local to the procedure, a parameter passed in from the calling procedure, or defined globally
Procedural Languages
Local data for each procedure are stored in an activation record associated with that procedure
data stored in such activation records typically have relatively simple types, such as integer, real, character, and Boolean
FORTRAN and C (examples) both have similar virtual computer and
execution characteristics
FORTRAN and C
FORTRAN is characterized by static storage allocation (activation records can be created during language translation) FORTRAN 90 changes this
C is characterized by dynamic activation record creation.
Both were designed for run-time efficiency
FORTRAN
Widely used for scientific and engineering computation
A text editor is used to create the program A FORTRAN compiler translates the
program into executable form. A linker is used to merge subprograms, the
main program, and run-time library routines into one executable program
Hello World in FORTRANPROGRAM TRIVIAL
INTEGER I
I = 2
IF( I .GE. 2 ) CALL PRINTIT
STOP
END
SUBROUTINE PRINTIT
PRINT *, “Hello World”
RETURN
END
FORTRAN Overview No run-time management is provided All storage is allocated statically before program
execution begins Only a restricted set of data types provided: four
types of numeric data( integer, real, complex, and double-precision real), Boolean data (called logical), arrays, character strings, and files
An extensive set of arithmetic operations and mathematical functions is provided
FORTRAN Overview
Relational and Boolean operations are provided
Simple selection from arrays using subscripting
Both sequential and direct-access files are supported
Flexible set of input-output facilities and format specification features are available
FORTRAN’s weakest point
Restricted data structuring facilities Essentially arrays and character strings of
fixed length No facilities for type definitions or data
abstraction. Subprograms provide the only abstraction
mechanism
Other weakness Statement sequence relies heavily on statement labels
and GOTO statements although each revision has added more nested control structures FORTRAN 77 added the IF … THEN … ELSE
Only two levels of referencing environment are provided: local and global the global environment may be divided into separate common
environments (called COMMON blocks) No special features are included to support construction of
large programs beyond the provision for independent compilation
FORTRAN example to sum an array PROGRAM MAIN
PARAMETER (MAXSIZ=99)
REAL A(MAXSIZ)
10 READ(5, 100, END=999) K
100 FORMAT(I5)
IF( K .LE. 0 .OR. K .GT. MAXSIZ) STOP
READ *, (A(I), I=1,K)
PRINT *, (A(I), I=1,K)
PRINT *, “SUM=“, SUM(A,K)
GO TO 10
999 PRINT *, “ALL DONE”
STOP
END
Labels are placed in columns 2 - 5
A C in column 1 indicates a comment
All statements require one line
unless column 6 has a nonblank
character, indicating continuation of
previous statement
Statements begin in column 7
Notes on FORTRAN main MAXSIZ is a programmer defined constant. The
value of 99 is actually what the translator uses. By default arrays begin at 1. K is undeclared so it is an integer 5 is used for keyboard input, 6 is used for display
output optional END=999 refers to end-of-file exception * denotes a list directed read which parses numbers
sequentially from the input stream FORTRAN 77 has no WHILE construct
Notes on FORTRAN main Variable names are 1 to 6 characters long,
begin with a letter, and contain letters and digits FORTRAN 90 - 31 chars long and also the _
FORTRAN is case insensitive blanks are ignored ( all 3 represent the same
statement) DO 20 I = 1, 20 DO20I=1,20 D O 2 0 I = 1 , 2 0
The FORTRAN Function SUM
C SUMMATION SUBPROGRAM
FUNCTION SUM(V,N)
REAL V(N)
SUM = 0.0
DO 20 I = 1, N
SUM = SUM + V(I)
20 CONTINUE
RETURN
END
Notes on FORTRAN SUM
Information from the main program is not used to pass information to the compiler. The erroneous line:
FUNCTION SUM (V, N, M) would also compile but may fail when the loader
tries to merge this subprogram with MAIN Although array is given as V(N), it refers to the
statically allocated parameter, real array A(99)
Language Evaluation Has amazing staying power Compiling techniques for arithmetic expression
evaluation, sparse matrix calculations and large array processing are excellent
numerical calculations that rely on computing derivatives, numerical integration, fluid dynamics, and solving for roots of complex equations - FORTRAN is as efficient as any language
Language Evaluation
Individual lines are easily understandable overall structure is opaque because the
heavy use of statement labels and GOTOs restriction of identifiers
C
Developed in 1972 by Dennis Ritchie and Ken Thompson of AT&T Bell Telephone Laboratories
Compact syntax and efficient execution characteristics have made it a popular systems programming language, even though it is a general-purpose language
C Hello World #include <stdio.h>
void main()
{
int i;
i=2;
if(i >= 2) printit();
}
void printit()
{ printf(“Hello World”);}
Semicolons now terminate statements - need no continuation marker
The C concept
You generally cannot just look at grammar, but at
the C language the C preprocessor the C interface assumptions (the h files) the C library
A C module
Consists of global declarations and a sequence of functions
multiple modules are linked together to form one executable program
each function may invoke other functions and access data local to that function or global
each function has a local activation record which is dynamic and allows for recursion
C function
Each function has access to global data there is no real block, thus activation records
do not need static links, only dynamic links
Data items Each data item has an efficient implementation Multi-dim arrays are built from one dimensional one dimensional have indices beginning with 0
which avoids the need for descriptors (attributes of data type)
the starting address of the array is the same as the virtual origin which avoids all complex array calculations
Data items C has pointers there is an equivalence between arrays and
pointers, permitting programs to use whichever method of access is most appropriate
Strings are implemented as arrays of characters, completely transparent so that strings may be access as strings, as arrays, or as pointers to individual characters
Other features
C has a large set of arithmetic operators C has a full set of control structures with very
flexible semantics C has a flexible type definition C has always been closely tied to OS
functionality ( in UNIX, example: malloc - OS function for allocating dynamic storage, but you use and include to do this)
Language Evaluation
Crisp, clear syntax close association of C data objects and
underlying machine architecture permits very efficient programs
large library
C Weakness Does permit very sloppy and error-prone
programming all enumerated types are integers, no strong typing break and switch are error-prone the == mistake use of call-by-value parameter passing when
pointers are needed to implement call-by-reference C has no real encapsulation
C popularity
It is flexible (any application) it is efficient (low-level semantics of
language) it is available (C compiler on most every
computer it is portable (you can write C program that
can execute on multiple platforms)
C assignment