Comparing BASIC with FORTRAN 77

0

If you grew up with computers like I did in the 1970s and 1980s, you probably learned a popular programming language for personal computers called BASIC, or the Beginner’s All-purpose Symbolic Instruction Code. You could find BASIC implementations on every personal computer of the era, including the TRS-80, Apple II, and the IBM PC. Back then, I learned AppleSoft BASIC on the Apple II before moving to GW-BASIC on the IBM PC and, later, to QuickBASIC on DOS.

But once upon a time, a popular language for scientific programming was FORTRAN, short for FORmula TRANslation. Although since the 1990 specification of the language, the name is more commonly stylized as “Fortran.”

When I studied physics as a university undergraduate student in the early 1990s, I used my experience in BASIC to learn FORTRAN 77. That was when I realized that BASIC derived many of its concepts from FORTRAN. To be clear, FORTRAN and BASIC differ in lots of other ways, but I found that knowing a little BASIC helped me to learn FORTRAN programming quickly.

I want to show some similarities between the two languages by writing the same program in both. I’ll explore the FOR loop in BASIC and FORTRAN 77 by writing a sample program to add a list of numbers from 1 to 10.

Bywater BASIC

BASIC came in many flavors, depending on your computer, but the overall language remained the same. One version of BASIC that I like is Bywater BASIC, an open source implementation of BASIC available for different platforms, including Linux and DOS.

To use Bywater BASIC on FreeDOS, you must first install the package from the FreeDOS 1.3 Bonus CD. To run it, go into the C: directory and type bwbasic. This command starts the BASIC interpreter. You can enter your program from this prompt:

bwBASIC:

Bywater BASIC uses an older BASIC programming standard that requires you to write every program instruction with a line number. Think of a line number like an index. You can easily refer to any instruction in the program with line numbers. As you type the program into the Bywater BASIC interpreter, add the line number before each instruction:

bwBASIC: 10 print "Add the numbers from 1 to 10 ..."
bwBASIC: 20 sum = 0
bwBASIC: 30 for i = 1 to 10
bwBASIC: 40 sum = sum + i
bwBASIC: 50 next i
bwBASIC: 60 print sum
bwBASIC: 70 end

Use the list command to view the program you have entered into the interpreter:

bwBASIC: list
10 print "Add the numbers from 1 to 10 ..."
20 sum = 0
30 for i = 1 to 10
40 sum = sum + i
50 next i
60 print sum
70 end

This short program demonstrates the FOR loop in BASIC. FOR is the most fundamental loop construct in any programming language, allowing you to iterate over a set of values. The general syntax of the FOR loop in Bywater BASIC looks like this:

FOR var = start TO end

In this example program, the instruction for i = 1 to 10 starts a loop that iterates through the values 1 to 10. At each pass through the loop, the variable i is set to the new value.

In BASIC, all instructions up to the next instruction are executed as part of the FOR loop. Because you can put one FOR loop inside another, Bywater BASIC uses the syntax NEXT variable to specify which loop variable to iterate.

Type run at the prompt to execute the program:

bwBASIC: run
Add the numbers from 1 to 10 ...
55

Bywater BASIC is called a BASIC interpreter because you can only run the program from inside the Bywater BASIC environment. This means the interpreter does all the hard work of interacting with the operating system, so your program doesn’t need to do that on its own, with the trade-off that the program runs a little slower in the interpreted environment than it might if it were a compiled program.

FreeBASIC

Another popular implementation of BASIC is FreeBASIC, an open source BASIC compiler for several platforms, including Linux and DOS. To use FreeBASIC on FreeDOS, you’ll need to install the FreeBASIC package from the FreeDOS 1.3 Bonus CD, then change into the C: directory where you’ll find the FreeBASIC programs.

FreeBASIC is a compiler, so you first create a source file with your program instructions, then run the compiler with the source code to create a program you can run. I wrote a similar version of the “add the numbers from 1 to 10” program as this BASIC file, which I saved as sum.bas:

dim sum as integer
dim i as integer
print "Add the numbers from 1 to 10 ..."
sum = 0
for i = 1 to 10
sum = sum + i
next
print sum
end

If you compare this code to the Bywater BASIC version of the program, you may notice that FreeBASIC doesn’t require line numbers. FreeBASIC implements a more modern version of BASIC that makes it easier to write programs without keeping track of line numbers.

Another key difference is that you must define or declare your variables in your source code. Use the DIM instruction to declare a variable in FreeBASIC, such as dim sum as integer, to define an integer variable called sum.

Now you can compile the BASIC program using fbc on the command line:

C:\DEVEL\FBC> fbc sum.bas

If your code doesn’t have any errors in it, the compiler generates a program that you can run. For example, my program is now called sum. Running my program adds up the numbers from 1 to 10:

C:\DEVEL\FBC> sum
Add the numbers from 1 to 10 ...
55

FORTRAN 77

The FORTRAN programming language is like a hybrid between old-style and modern BASIC. FORTRAN came before BASIC, and BASIC clearly took inspiration from FORTRAN, just as later versions of FORTRAN took cues from BASIC. You write FORTRAN programs as source code in a file but you don’t use line numbers everywhere. However, FORTRAN 77 does use line numbers (called labels) for certain instructions, including the FOR loop. Although in FORTRAN 77, the FOR is actually called a DO loop, it does the same thing and has almost the same usage.

In FORTRAN 77, the DO loop syntax looks like this:

      DO label var = start, end

This situation is one of the instances where you need a line number to indicate where the DO loop ends. We used a NEXT instruction in BASIC, but FORTRAN requires a line label instead. Typically, that line is a CONTINUE instruction.

Look at this sample FORTRAN program to see how to use DO to loop over a set of numbers. I saved this source file as sum.f:

      PROGRAM MAIN
      INTEGER SUM,I
      PRINT *, 'ADD THE NUMBERS FROM 1 TO 10 ...'
      SUM = 0
      DO 10 I = 1, 10
        SUM = SUM + I
   10 CONTINUE
      PRINT *, SUM
      END

In FORTRAN, every program needs to start with the PROGRAM instruction, with a name for the program. You might name this program SUM, but then you cannot use the variable SUM later in the program. When I learned FORTRAN, I borrowed from C programming and started all of my FORTRAN programs with PROGRAM MAIN, like the main() function in C programs, because I was unlikely to use a variable called MAIN.

The DO loop in FORTRAN is similar to the FOR loop in BASIC. It iterates over values from 1 to 10. The variable I gets the new value at each pass over the loop. This allows you to add each number from 1 to 10 and print the sum when you’re done.

You can find FORTRAN compilers for every platform, including Linux and DOS. FreeDOS 1.3 includes the OpenWatcom FORTRAN compiler on the Bonus CD. On Linux, you may need to install a package to install GNU Fortran support in the GNU Compiler Collection (GCC). On Fedora Linux, you use the following command to add GNU Fortran support:

$ sudo dnf install gcc-gfortran

Then you can compile sum.f and run the program with these commands:

$ gfortran -o sum sum.f
$ ./sum
ADD THE NUMBERS FROM 1 TO 10 ...
55

A few differences

I find that FORTRAN and BASIC are very similar, but with some differences. The core languages are different, but if you know a little of BASIC, you can learn FORTRAN. And if you know some FORTRAN, you can learn BASIC.

If you want to explore both of these languages, here are a few things to keep in mind:

FORTRAN 77 uses all uppercase, but later versions of FORTRAN allow mixed cases as long as you use the same capitalization for variables, functions, and subroutines. Most implementations of BASIC are case-insensitive, meaning you can freely mix uppercase and lowercase letters.

There are many different versions of BASIC, but they usually do the same thing. If you learn one BASIC implementation, you can easily learn how to use a different one. Watch for warnings or error messages from the BASIC interpreter or compiler, and explore the manual to find the differences.

Some BASIC implementations require line numbers, such as Bywater BASIC and GW-BASIC. More modern BASIC versions allow you to write programs without line numbers. FreeBASIC requires the -lang deprecated option to compile programs with line numbers.

This article is adapted from BASIC vs. FORTRAN 77: Comparing programming blasts from the past by Jim Hall, and is republished with the author’s permission.