Fortran is a full procedural language.
Execution Entry PointEdit
The main entry point to a program is at a
Fortran was designed with punch card in mind, hence the actual Fortran code start on the 7th character offset. Characters are prefix by 6 characters. Characters 1 to 5 indicate a label, character 6 indicates a line continuation.
12345 PROGRAM +HLO WLD 67890 PRINT *, +'HELLO WORLD' 99999 END
PROGRAM and the <END> statement are optional in this example.
- The characters from postion 76 onward are ignored. So be careful when you insert a character into a long line. (IBM FORT/VS compiler used to insert line numbers in this column for some odd reason)
- The space character is ignored, except in quotes, most notably these two lines are interpreted the same, hence the suspicion relating to the Mariner crash.
DO 19,I=1,99 DO19I=1,99
A 'C', '*' and sometimes a 'c' character in the first column indicates a comment. For example:
* This is a hello world program 12345 PROGRAM +HLO WLD C Output Hello World 67890 PRINT *, +'HELLO WORLD' c all done, I'm out of here... 99999 END
In Fortran the type of a variable is implicitly defined by the first letter of the variable name. Variables beginning with the letters I through N were automatically considered to be integers, while A through H and O through Z were considered to be real numbers.
This behavior can be modified with an IMPLICIT INTEGER declaration (setting the letter range of integers) or an explicit declaration of a variable giving its type.
Hence "GOD is REAL (unless declared INTEGER)."
PROGRAM EG DEC * Net line is not needed as this would be IMPLICIT INTEGER INTEGER I,J,K * long INTEGERs INTEGER II*8,JJ*8,KK*8 * define some arrays INTEGER M(2),N(3),O(4) * Some array initialisations INTEGER MM(2)/1,2/,NNN(3)/1,2,3/,OOOO(4)/1,2,3,4/ * Some REAL initialisations REAL R/1.0/,S/2.0/,T/3.0/ * Some DOUBLE PRECISION (long) initialisations REAL*8 RR/1.0D/ DOUBLE PRECISION SS/2.0D/ END
Fortran has several kind of
PROGRAM P PRINT *,'The cube of 2 is',CUBE(2.0) PRINT *,'The square of 2 is',SQUARE(2.0) END FUNCTION CUBE(X) * FILSQ is an inline Function returning the square of a value FILSQ(X)=X*X CUBE=FILSQ(X)*X RETURN ENTRY SQUARE(X) CUBE=FILSQ(X) END
The cube of 2 is 8. The square of 2 is 4.
Note that in the output the first character is normally a blank character. By inserting other characters in the first column you can force the printer to do form feeds, and perform other interesting behaviors.
The scope of variable was strictly limited to the current PROGRAM/SUBROUTINE or DATA BLOCK. DATA BLOCK can be shared between PROGRAM/SUBROUTINEs, allowed the programmer to extend the scope of a variable.
<Describe the conditional statements in text and present.>
* A one liner IF(1 .EQ. 2)PRINT *,'Dreaming' * A block structured IF(1 .EQ. 1)THEN PRINT *,'A distinct possibility' ELIF(2 .EQ. 2)THEN PRINT *,'Too late' ELSE PRINT *,'Way too late' ENDIF *
Conditional statements also include GOTO statements.
I=3 GOTO(111,112,113,114)I PRINT *,'I was not found' GOTO 119 111 CONTINUE print *,'I was 1' GOTO 119 112 CONTINUE print *,'I was 2' GOTO 119 113 CONTINUE print *,'I was 3' GOTO 119 114 CONTINUE print *,'I was 4' GOTO 119 119 CONTINUE
FORTRAN also has a 3 way condition.
I=1 IF(I)121,122,123 121 CONTINUE print *,'I is less then zero' GOTO 129 122 CONTINUE print *,'I is zero' GOTO 129 123 CONTINUE print *,'I is greater then zero' GOTO 129 129 CONTINUE
FORTRAN 77 > DO / WHILE LOOP
A = 1 DO WHILE (A .LT. 10) PRINT *, 'COUNTING: ', A A = A + 1 END DO
In older FORTRANS labels and GOTOs would be used to the same effect. For example:
INTEGER A IF(.NOT.(A .LT. 10))GO TO 139 PRINT *,'COUNTING: ',A A = A + 1 * A convention is to terminate the label number with a 9 * in cases where the GOTO breaks out of a loop. 139 CONTINUE
Example one hello world:
PRINT *,'Hello World'
WRITE (*,*)'Hello World'
Or (The stand output channel is 6):
WRITE (6,*)'Hello World'
WRITE (6,'( A)')'Hello World'
WRITE (UNIT=6,FMT='( A)')'Hello World'
WRITE (UNIT=6,FMT=141)'Hello World' 141 FORMAT(A)
Fortran includes OPEN, CLOSE, READ, WRITE and PRINT statements. The READ and WRITE commands could access recored in a file by record number, hence Fortran contains a semblence of random file access. Unfortunately on File IO the size of a "word" is implementation dependent, and ranges from 6 bits on CDC, 8bits on IBM and 32 bits on VAXVMS.
FORTRAN 77 has no structure or class definition. The closest example was the COMMON statement.
CHARACTER NAME*16, ADDRESS*64 FLOAT BALANCE*4 COMMOM /CSTREC/NAME,ADDRESS,BALANCE
An EQUIVALENCE statement allows such COMMON block statement be be overlayed, and allow direct assignment. But this is problematic.
CHARACTER ALLREC*(16+64+4) EQUIVALENCE(ALLREC,CUSREC)
An EQUIVALENCE also have a similar effect as the union from Algol68 (and then C) language union statement. The net effect is two variables ALLREC,CUSREC share the same location in memory.
Access to other languages was only available if the language supported FORTRANs SUBROUTINE calling conventions (Not all FORTRANs used the stack!). And the actual conventions varied from machine to machine.
Garbage collection is not part of the language, but has been implemented separately using datablock. In Fortran the memory at program startup was all that would be available to the program until it terminated. (The exception would be if the compiler supported recursion with local variables, but this was not always the case.)
external scoping rules of Fortran is similar to the scoping found in the C programming language. However Fortran 77 does not have a standard
- DATABLOCK EXTNM1
- EXTERNAL EXTNM2
- PROGRAM EXTNM3
- FUNCTION EXTNM3
- ENTRY EXTNM4
- SUBROUTINE EXTNM5
- DATA BLOCK
- COMMON EXTNM6
- Arrays are indexed starting with 1.
Books and ArticlesEdit
<List additional books and articles that may be helpful. Please include for what level reader the references are appropriate. (beginner/intermediate/advanced)>