FORTRAN Version of the Ohm's Law Program
User Rating: / 0
PoorBest 
Written by Bryce Ringwood   
This subject appears surprisingly often in web searches on my pages. It seems that FORTRAN is a language that is not dead yet. This is an admittedly very superficial treatment of the subject.
 
FORTRAN stands for "Formula Translator". It is a computer programming language developed by John W Backus in the early 1950's. The first manual for application on the IBM 704 was produced in 1956. I have written FORTRAN in capitals, because it used to be a convention that programming languages with names that were an acronym were written in upper case, whilst languages named after people were written in lower case, with an initial capital, e.g. "Pascal", "Ada". The IBM 704 used vacuum tube or "valve" circuitry and was said to be capable of almost 13 000 floating point operations (FLOPS) per second. This computer performs something close to: 23 000 000 000 floating point operations per second, as a rough comparison.
 
Prior to the introduction of FORTRAN, computer programs were written in assembly language. Each computer or microcontroller has  its own variety of assembler, which consists of simple instructions to load and store data, as well as logical or arithmetic operations on it. For a human programmer, it is somewhat tedious.
 
The FORTRAN compiler takes English language-like instructions and compiles them into assembly language, then calls the assembler to convert the assembly into machine code and lists it in a file on disk called in an "object module". The object module won't run as an executable (.exe) because it does not know where to start in memory when the program is invoked. A separate step called "linking" provides a start address and combines all the separate object modules (there may be many of these in a large program) into a single executable. Static linking binds all the modules into one executable. Dynamic linking allows the program to call modules as needed. Dynamic link libraries (.dll) in Windows contain the objects that may or may not be needed at run time. If you forget to put the .dlls on disk, or register them, then your program fails with an error and/or your client shouts at you over the phone.
 
Note that FORTRAN is usually used for batch processing with input and output files. It IS possible to write GUI programs a la Charles Petzold, but don't do it.Programming Proverbs
 

Obtaining your Fortran Compiler

 
For the purpose of this exercise, I am using Windows 10 with the Watcom Fortran77 compiler. I will also use GNU Fortran, gfortran, which also runs on Linux. The compilers are available as free downloads from openwatcom.org and mingw.org. Of course, if you are running Linux on a Raspberry Pi, you don't need to do anything. The only problem I had with MinGW was a missing .dll (see above!). Make a note of the file in question - it will be located in C:\mingw\bin (probably) and add the path to your missing file using the Windows control panel.
 
Open Watcom has its own IDE (Integrated development environment), which seems to work on Windows 10. You can use "Codeblocks" for gfortran.
 

FORTRAN Versions

 
There are many different versions of FORTRAN. I will be using Fortran 77 because it retains some of the awkward syntax and coding procedures of the earliest Fortrans. It was also fairly portable - programs written on a Sperry 1100 would work on a PC using Miicrosoft's compiler, without much hassle.
 
This version also allowed the use of some structured programming elements. The thinking behind each new advance in programming being the reduction of mistake making and ease of program maintenance. Nowadays, we are more worried about hackers and security.
 
Regardless of what computer language you are using-just try to keep each program module short enough to fit on a single sheet of A4 paper. Write BIG!
 

FORTRAN Essentials

 
Variables
 
Variable names in FORTRAN 77 can be of any reasonable length (compiler dependent) and may be in upper or lower case. They may not begin with a numeral. The underscore is allowed. I don't advise giving your variables names things like 'character' or 'if' or combinations thereof. In any case, the compiler may not allow it. Some do.
 
Data Types
 
FORTRAN distinguishes  between integer and floating point numbers. There is a further distinction according to storage and precision. FORTRAN also has a complex number data type.
 
Implicitly, variables whose names begin with the characters I,J,K,L,M,N are of INTEGER data type. Variables whose names begin with the remaining letters of the alphabet are of REAL data type. 
 
REAL numbers have a precision of somewhere around 7 digits. If you need more precision, the the DOUBLE PRECISION data type will provide much more - between 15 and 16 digits. These may vary, depending on the compiler you use.
 
There is also a LOGICAL data type.
 
The Operators
 
These are +,-,/ and **, for addition, subtraction, division, multiplication and exponentiation. + and - are also unary operators, so the expression A= -3+B is valid.
The logical operators are .NOT., .AND., .OR., .EQV. and .NEQV. Some compilers have a .XOR. When doing comparisons in an IF statement, the following relational operators can be used:- .LT. (Less than), .GT. (Greater than) , .EQ. (Equals to), .NE. (Not equals), .GT. (Greater than) and .LE. (Less than).
 
Statements
 
FORTRANS earlier than FORTRAN 90 (I think) use a fixed format. Columns 1 to 5 are used for Labels. Column 6 is normally left empty, but if the statement exceeds 72 characters, a character is placed in column 6 as a continuation character. Statements begin in column 7 and end in column 72. Anything in columns 73 and beyond is ignored. Remember - FORTRAN programs were originally put on to decks of punched cards.
Lines beginning with the letter 'C' in the first column are comment lines.
 
Transfer of control
 
This is performed using the infamous GO TO statement. A particularly delightful feature is the computed GO TO, of the form GOTO(10,20,30,70,999)NEXT. The program jumps to the label corresponding to the value of NEXT. For example if NEXT = 2, then the program would jump to label 20. If NEXT = 5, the program would jump to label 999. 
 
Program looping is performed by a DO loop, of the form DO 20, IFIRST,ILAST,INCREMENT. The program will repeat ILAST-IFIRST/INCREMENT times.  Example:
123456789012345678901234567890 
      I=0
      DO 20, 1,10,1
              I=I+1
20    CONTINUE
 
There is a DO ... WHILE structure in FORTRAN 77.
 
Conditional branching is performed using the IF statement (see example in the OHM program). FORTRAN 77 also has an IF ... THEN ... ELSE structure that allows you to avoid the use of labels.
 
FORTRAN input and output    
 
This is a very big subject. Basically, there are READ, WRITE and PRINT statements. These may be associated with a FORMAT statement:
 
12345678901234567890
      READ(5,1000)I,V
1000  FORMAT(I5, F5.2)
      WRITE(6,1050)I,V
1050  FORMAT(1H0,I5,F5.2)
       ...
I is an INTEGER, V is REAL. The 5 in the read statement is the unit number for the console. Other unit numbers may be for files, line printers (Back in the day, we had line printers - noisy chain driven things) and so on. The 1000 in the statement is the label of the associated FORMAT statement. The I5 is formatting for an integer up to 5 digits in length. The F5.2 signifies a REAL with 5 total digits in length including the decimal point, having two digits after the decimal point. For example 43.52.
 
On output, the WRITE statement is similar, but unit 6 is the console. The first character 1H0 of the output is a carriage control character. In early FORTRAN, the inverted comma had yet to be invented and characters were encoded in Hollerith format, such as NAME = 4HFRED. I could have written '0' instead of 1H0, but old habits die hard.
 
Arrays
 
FORTRAN handles arrays by declaring them with a DIMENSION statement.
 
DIMENSION a_Matrix (3,3)
 
                  a_Matrix(1,2) = 7.997
...
 
See also the FORTRAN reference.
Functions and Subroutines
 
A subroutine is called with the CALL statement
 
 12345678901234567890 
      I=5
      CALL FOO(I)
      PRINT*,'I= ',I
    
      SUBROUTINE FOO(J)
      J=3
      RETURN
 
A function is defined as per the example below
 
      I=5
      M=FOO(I)
C-----This is not a misprint!
      PRINT*,'I= ',I   
 
      INTEGER FUNCTION FOO(J)
      J=3
C-----You have to give the function a return value, otherwise the computer will complain
      FOO=J
      RETURN
 
Try these out. Note that unlike 'C', FORTRAN passes values by reference. The value of the input variables may be changed by the subroutine, as happens here. There could be unintended consequences. Note that in FORTRAN 77, a function or subroutine can not call itself. In 'C' arguments are passed by value. 
 
 
A FORTRAN program
 
Now that we know (a little bit of) FORTRAN, here is a simple program to evaluate the missing Voltage, Current or Resistance in a simple electric circuit:
 
       PROGRAM OHM
 
C----- PROGRAM TO COMPUTE A SIMPLE ELECTRICAL CIRCUIT
 
       REAL V,I,R,P
999    WRITE(*,1000)
1000   FORMAT(1H ,'ENTER V,I AND R. THE UNKNOWN MUST BE ZERO')
       READ(*,1050)V,I,R
1050   FORMAT(3F5.2)
 
       IF(V .EQ. 0)GO TO 10
       IF(I .EQ. 0)GO TO 20
       IF(R .EQ. 0)GO TO 30
 
       PRINT*,'YOU DIDN''T ENTER A ZERO !'    
       GO TO 999
 
10     V = I*R
       GO TO 40
20     I=V/R
       GO TO 40
30     R=V/I
 
40     P = I*V
       WRITE(*,1100) V,I,R,P
1100   FORMAT(1H ,'V,I,R AND POWER = ',4F5.2 )
       STOP
       END
 
 
 
The program has a number of flaws, but is typical of a small program you might write in a hurry to solve a simple problem or perform a small calculation.
 
Compile it with Watcom, or
gfortran ohm.for -o ohm.exe
 
Run it using 
C:\ohm  (if on Windows.)
 
Be sure to include the decimal points on the numbers you enter!!
Now run it again, but enter stupid values, such as 'fred',john' and '0'. The program will generate a run-time error.
 
Moving On
 
I deliberately wrote the OHM program in an almost 1950's style to demonstrate a way of programming that is really out of fashion. After studying a proper programming reference or text book (available on line), you should be able to remove most of the labels and all of the GO TOs. Rather more challenging - can you make the program operate in such a way that it will accept any input and degrade gracefully instead of giving a run-time error ?
 
I don't use FORTRAN any more. I principally use 'C' because that is the language used by microprocessors for my small electronics projects. If I need to do a calculation, I might use javascript and share it with you on theses web pages. I also use HAMCALC written in BASIC for designing transformers, and Calcpad for general calculations.  Maybe I will use it again, now that I have written this article.
 
FORTRAN is possibly still the language of choice for very complicated scientific and analytical programs. It is very efficient and there are a great number of numerical analysis "packages" available to assist with complex problems. FORTRAN continues to be developed - I think the next version is going to be available in 2017. 

References

1. Microsoft FORTRAN reference manual. 

2. Ledgard H, Chimura L - "FORTRAN WITH STYLE", Hayden 1978

 
Joomla template by a4joomla