Download presentation
Presentation is loading. Please wait.
Published byCecil Moody Modified over 9 years ago
1
Simple Procedural Languages: FORTRAN and C Lecture 9: Dolores Zage
2
Procedural Languages n Are the closest thing to “typical” programming language for demonstrating common language features. n Language consists of a series of procedures that execute when called n Each procedure consists of a sequence of statements n Each statement manipulates data that either is local to the procedure, a parameter passed in from the calling procedure, or defined globally
3
Procedural Languages n Local data for each procedure are stored in an activation record associated with that procedure n data stored in such activation records typically have relatively simple types, such as integer, real, character, and Boolean n FORTRAN and C (examples) n both have similar virtual computer and execution characteristics
4
FORTRAN and C n FORTRAN is characterized by static storage allocation (activation records can be created during language translation) FORTRAN 90 changes this n C is characterized by dynamic activation record creation. n Both were designed for run-time efficiency
5
FORTRAN n Widely used for scientific and engineering computation n A text editor is used to create the program n A FORTRAN compiler translates the program into executable form. n A linker is used to merge subprograms, the main program, and run-time library routines into one executable program
6
Hello World in FORTRAN PROGRAM TRIVIAL INTEGER I I = 2 IF( I.GE. 2 ) CALL PRINTIT STOP END SUBROUTINE PRINTIT PRINT *, “Hello World” RETURN END
7
FORTRAN Overview n No run-time management is provided n All storage is allocated statically before program execution begins n 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 n An extensive set of arithmetic operations and mathematical functions is provided
8
FORTRAN Overview n Relational and Boolean operations are provided n Simple selection from arrays using subscripting n Both sequential and direct-access files are supported n Flexible set of input-output facilities and format specification features are available
9
FORTRAN’s weakest point n Restricted data structuring facilities n Essentially arrays and character strings of fixed length n No facilities for type definitions or data abstraction. n Subprograms provide the only abstraction mechanism
10
Other weakness n Statement sequence relies heavily on statement labels and GOTO statements u although each revision has added more nested control structures u FORTRAN 77 added the IF … THEN … ELSE n Only two levels of referencing environment are provided: local and global u the global environment may be divided into separate common environments (called COMMON blocks) n No special features are included to support construction of large programs beyond the provision for independent compilation
11
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
12
Notes on FORTRAN main u MAXSIZ is a programmer defined constant. The value of 99 is actually what the translator uses. u By default arrays begin at 1. u K is undeclared so it is an integer u 5 is used for keyboard input, 6 is used for display output u optional END=999 refers to end-of-file exception u * denotes a list directed read which parses numbers sequentially from the input stream u FORTRAN 77 has no WHILE construct
13
Notes on FORTRAN main n Variable names are 1 to 6 characters long, begin with a letter, and contain letters and digits u FORTRAN 90 - 31 chars long and also the _ n FORTRAN is case insensitive n blanks are ignored ( all 3 represent the same statement) u DO 20 I = 1, 20
14
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
15
Notes on FORTRAN SUM n Information from the main program is not used to pass information to the compiler. The erroneous line: n FUNCTION SUM (V, N, M) n would also compile but may fail when the loader tries to merge this subprogram with MAIN n Although array is given as V(N), it refers to the statically allocated parameter, real array A(99)
16
Language Evaluation n Has amazing staying power n Compiling techniques for arithmetic expression evaluation, sparse matrix calculations and large array processing are excellent n 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
17
Language Evaluation n Individual lines are easily understandable n overall structure is opaque because the heavy use of statement labels and GOTOs n restriction of identifiers
18
C n Developed in 1972 by Dennis Ritchie and Ken Thompson of AT&T Bell Telephone Laboratories n Compact syntax and efficient execution characteristics have made it a popular systems programming language, even though it is a general-purpose language
19
C Hello World #include void main() { int i; i=2; if(i >= 2) printit(); } void printit() { printf(“Hello World”);} Semicolons now terminate statements - need no continuation marker
20
The C concept n You generally cannot just look at grammar, but at n the C language n the C preprocessor n the C interface assumptions (the h files) n the C library
21
A C module n Consists of global declarations and a sequence of functions n multiple modules are linked together to form one executable program n each function may invoke other functions and access data local to that function or global n each function has a local activation record which is dynamic and allows for recursion
22
C function n Each function has access to global data n there is no real block, thus activation records do not need static links, only dynamic links
23
Data items n Each data item has an efficient implementation n Multi-dim arrays are built from one dimensional n one dimensional have indices beginning with 0 which avoids the need for descriptors (attributes of data type) n the starting address of the array is the same as the virtual origin which avoids all complex array calculations
24
Data items n C has pointers n there is an equivalence between arrays and pointers, permitting programs to use whichever method of access is most appropriate n 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
25
Other features n C has a large set of arithmetic operators n C has a full set of control structures with very flexible semantics n C has a flexible type definition n 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)
26
Language Evaluation n Crisp, clear syntax n close association of C data objects and underlying machine architecture permits very efficient programs n large library
27
C Weakness n Does permit very sloppy and error-prone programming n all enumerated types are integers, no strong typing n break and switch are error-prone n the == mistake n use of call-by-value parameter passing when pointers are needed to implement call-by- reference n C has no real encapsulation
28
C popularity n It is flexible (any application) n it is efficient (low-level semantics of language) n it is available (C compiler on most every computer n it is portable (you can write C program that can execute on multiple platforms)
29
C assignment
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.