Fortran: CS4100 Dr. Martin
Fortran: CS4100 Dr. Martin
Highlights of Psuedo-Code
• Virtual computer
– More regularity
1
9/4/13
FORTRAN timeline
FORTRAN
• 1954: Project approved
• 1957: FORTRAN
– First version released
• Goals
• 1958: FORTRAN II and III – Decrease programming costs (to IBM)
– Still many dependencies on IBM 704
– Efficiency
• 1962: FORTRAN IV
– “ANS FORTRAN” by American National Standards Institute
– Breaks machine dependence
– Few implementations follow the specifications
• We’ll look at 1966 ANS FORTRAN
Subprogram n
2
9/4/13
3
9/4/13
n-way Branching
Reversing TRUE and FALSE with Computed GOTO
GOTO (L1, L2, L3, L4 ), I
10 case 1
GOTO 100
• To get if-then-else –style if: 20 case 2
GOTO 100
IF (.NOT. (condition)) GOTO 100 30 case 3
GOTO 100
case for true 40 case 4
GOTO 200 GOTO 100
100
100 case for false • Transfer control to label Lk if I contains k
200 • Jump Table
4
9/4/13
5
9/4/13
6
9/4/13
Subprograms Subprograms
• AKA subroutine
– User defined
SUBROUTINE Name(formals) • When invoked
…body…
– Function returns a value – Using call stmt
RETURN
• Can be used in an expression – Formals bound to
END
• Important, late addition actuals
• Why are they important? … – Formals aka dummy
– Subprograms define procedural abstractions CALL Name (actuals) variables
– Repeated code can be abstracted out, variables
formalized
– Allow large programs to be modularized
• Humans can only remember a few things at a time
(about 7)
7
9/4/13
A Dangerous Side-Effect
Parameter Passing
• What if parameter passed in is not a variable?
SUBROUTINE SWITCH (N)
• Pass by reference N = 3
– On chance may need to write to RETURN
• all vars passed by reference END
– Pass the address of the variable, not its value …
– Advantage: CALL SWITCH (2)
• Faster for larger (aggregate) data constructs • The literal 2 can be changed to the literal 3 in FORTRAN’s
• Allows output parameters literal table!!!
– Disadvantage: – I = 2 + 2 I = 6????
• Address has to be de-referenced – Violates security principle
– Not by programmer—still, an additional operation
• Values can be modified by subprogram
• Need to pass size for data constructs - if wrong?
8
9/4/13
9
9/4/13
10
9/4/13
Optimizations Subscripts
• Subscripts can be expressions
• Arrays are mostly associated with loops – A(i+m*c)
– Most programmers initialize controlled variable to 1, and – This defeats above optimization
reference array A(i) – Therefore, subscripts are limited to
– Optimization: • c and c’ are integers, v is an integer variable
• c
• Initialize controlled variable to address of array element
• v
• Therefore, we’ll increment address itself
• v+c, v-c
• Dereference controlled variable to get array element • c*v
• c*v+c’, c*v-c’
– A(J - 1) ok; A(1+J) not ok
• Optimizations like this sold FORTRAN
Optional Declaration
Now: Semantics (meaning)
• FORTRAN does not require variables to be declared
– First use will declare a variable
• “They went to the bank of the Rio
• What’s wrong with this?
– COUNT = COUMT + 1 Grande.”
– What if first use is not assignment?
• What does this mean?
• Convention:
– Variables starting with letters i, j, k, l, m, n are integers • How do we know?
– Others are floating point
– Bad practice: Encourages funny names (KOUNT, ISUM, • CONTEXT, CONTEXT, CONTEXT
XLENGTH…)
11
9/4/13
12
9/4/13
EQUIVALENCE EQUIVALENCE
• Since dynamic memory allocation is not
• This is only to be used when usage of
supported, and memory is scarce, INDATA and RESULT do not overlap
FORTRAN has EQUIVALENCE • Allows access to different data types (float as
if it was integer, etc.)
DIMENSION INDATA(10000), RESULT(8000)
EQUIVALENCE INDATA(1), RESULT(8) • Has same dangers as COMMON
13
9/4/13
14
9/4/13
Some Highlights
• Arrays
– Only data structure
– Data constructor
– Static
– Limited to three dimensions
– Restrictions on index expressions
– Optimized
– Column major order for 2-dimensional
– Not required to be initialized
15