It appears that my next research duties will involve working with a professor’s old FORTRAN code. Now, I’m a capable programmer, and I’m familiar with several languages (mostly C), but I’ve never done any FORTRAN programming. What books/webpages/other sources would be good references for me to use to learn the language? Is there a standard resource, like Kernigan and Ritchie’s C book? What differences will stand out?
If it matters, I suspect the code is f77, but it could be some other version.
If you’re an experienced C programmer, you should be able to pick up Fortran pretty easily. The syntax is similar and nothing terribly cryptic.
Unfortunately, there isn’t a “Kernighan and Ritchie” like reference. But there are a lot of books out there. Fortran 77 was a popular standard for a number of years, but lacked a lot of the bells and whistles that were added.
The biggest differences off the top of my head(keeping in mind that it has been a while since I’ve worked with Fortran):
strict column placement (no “as much whitespace as you want”): line numbers (if any) must be in columns 1 - 6 (I think), continuation ‘X’ in 7, and the statement starts in 8.
No structures. Just arrays (multi-dimensional, but all same type)
No pointers.
Implied typing: variables starting with “i”, “k” are integers (fixed point math), variables starting with other letters are floating point.
not a “structured” language - aka no brackets and the dreaded “goto” statement. You will learn (hopefully minimally painfully) why they abolished the “goto” statement in suceeding languages.
So the syntax and such will not be so different that you won’t be able to figure out what’s going on in a given statement. BUT Fortran lended itself to the infamous “spaghetti code” phenomenon. That is, being unstructured, it allowed programmers to write the most difficult to follow code possible. Most good programmers did things in reasonable manners, but inevitably you would come across code that jumped from hell and back (or sometimes not “back”). This is going to be the biggest challenge - figuring out what the code is doing. You thought reading someone else’s C code was tough ?
Somthing to keep in mind is that Fortran (“formula translation”) was originally created to implement mathematical calculations: step 1 → step 2 → step 3 → etc… And for this, the code can be reasonable readable. When programs started to get more complicated and they needed to start writing more “control” functions/applications, that’s when things got ugly (spaghetti code).
Good luck. If you get stumped on something, you could e-mail me the code and I can maybe help (no promises).
Variables starting with I through N are implicitly INtegers.
Case doesn’t matter in variable names, so cost, COST and CoSt are all the same variable. (Strictly, only upper case is valid, but that’s never enforced.)
GOTOs are needed if you want things like Do While or Repeat Until loops, since they aren’t part of the language, and you have to make them yourself.
Even experienced programmers who have seen dozens of languages will need to understand the details of COMMON blocks (ugly implementation of sharing global variables among program segments and passing parameters). And the EQUIVALENCE weirdness too.
A couple of other differences: The first element of an array is 1, not 0. Multidimensional arrays are stored in memory with the left-most dimension varying most rapidly, instead of the rightmost dimension.
By the way, there’s no odd behavior that can’t be caused by writing past the end of an array. If you’re debugging, and find yourself saying “How can that even happen?”, you’ve probably written past the end of an array.
As far as books, no one buys Fortran books anymore. I’ve found reference books released for free as PDFs (or maybe postscript) by their authors because of this.
I was already aware of the unit-offset arrays and pass-by-reference (by the way, is it still possible to redefine the value of 2?). It’s been said that a true C programmer can write C code in any language, and a true FORTRAN programmer can write FORTRAN code in any language, and although I’ve never actually used FORTRAN, I have seen FORTRAN code written in C (coughNumerical Recipescough).
Probably not an option… The professor who wrote this code is very protective of it, and probably would not approve of me releasing it “into the wild”.
This is very true, and it bears remembering. If you are foolish enough to write past the end of an array, it is perfectly compliant with the standard for the compiler to cause demons to fly out of your nose.
You poor bastard. I’ve done way too much Fortran 77 programming, mostly back in my grad school days. It’s not a terrible language, just primitive and kind of annoying. There are a few irritations which will make you appreciate C/C++:
Depending on how strict the compiler is, if you don’t keep an eye on how long your lines are it may just cut them off at character 80. So you have to use continuation characters, which are just retarded. I think you can set up Emacs to know that a certain character is a continuation and automatically stick it in the right column – I just got good at hitting the space bar the right number of times, to the point that I didn’t even think about it.
Text formatting is pretty horrible, as is just about all I/O.
Implicit typecasting based on what letter the variable starts with. I highly recommend putting “implicit none” at the head of all your programs, which will force you to explicitly declare everything.
You also want to check the loop ordering for nest loops involving multidimensional arrays, if you have any. Academic Fortran programmers love to put them in the wrong order, which gives a noticeable performance hit.
Character 72 actually. This tutorial is the one I used to learn the language. Dunno if its the best out there or anything, but it did the job, and one of the nicer things about Fortran 77 is that there isn’t much to it, learn how to do loops, conditionals, IO and the already mentioned (horrible) common blocks and subroutines and you’ve basically seen 99% of the language.
Here’s a more up-to-date version of the textbook I used. I had the ANSI 68 version, but this one has all the fancy, new-fangled FORTRAN77 extensions in it; it’s the same author as mine. I found the earlier edition quite clear and well organized; I’d expect the same from this one.
Agreed. It’s what we learn in my “Computing for Engineers” course for just that reason (Well, Fortran 90, but you get the point.) Very straightforward, really.