I am a newcomer in Fortran. My background is a bit of Pascal (back in Turbo Pascal days in the early '90s, as a University student), I have a good command of Mathematica (symbolic Mathematics exclusively) and I have a few failed attempts to learn languages like C (many corner cases and too cryptic and complex for my dyslexia) and Nim (quite overwhelming in features).
So I decided to give Fortran a try -as a hobby!
I have two questions:
Do you have any learning resources (I would prefer books, for I am old schoolâŚ)?
For a start I have âIntroduction to programmingâ by Ian Chivers and Jane Sleightholme.
I am open to any suggestions (including other books and, perhaps, online tutorials)
and a naive question
I tried this:
program help
implicit none
print *, "Hello World!"
end program
save as example.f90, then upon compiling (I have Void Linux @64bit and GNU Fortran)
$ gfortran example.f90 -o example
I get:
$ ./example
Hello World!
Is there any explanation for the space before the Hello World?
Donât be impressed by the subtitle or the Chapter 1 (which exposes what you will know at the end of the book). The Chapter 2 will take you by the hand, starting from the Hello world example. The book is very progressively rising.
An approach complementary to print is to use write, e.g.
program help
implicit none
write (*, "(A)") "Hello World!"
end program
Arguably, this one is is easier to then adjust the destination of the output (the unit, here * symbolizes the default [typically the command line], though a different entry can address for instance an open file). And second, to define the format of the output, especially if the output consists of multiple (instances of) data types; e.g. (3(F8.5, 1X) I3.3) reports about three reals between to -9.99999 and 99.99999 separated each by a single space, followed by an integer up to 999 padded with leading zeroes (like 018) if the later is less than 100.
In the Olden Days of computing, there were big, noisy, machines called âline printersâ. Youâd position a big box of fan-folded paper under them, and an electro-mechanical âprint trainâ would hammer out characters on each line. Yes, they were very noisy. In fact, in some cases one often had to wear hearing protection around them.
Column 1 was used for vertical âcarriage controlâ. You could put a â1â in column 1 to skip to the next page, a â+â to overprint a line, and a â0â to double space. These were actually specified in the various Fortran Standards, up through Fortran 95. Since all of this has been obsolete since the 1980s, it simply became an annoyance. In Fortran 2003 they were deleted from the Standard.
will not have several behaviors that vary between compilers that list-directed I/O has(that is, I/O where the format is an asterisk). These include the amount of white-space between items, and at what line length line-breaks are added. It also gets rid of the leading space on the line. It does not print complex values as nicely as list-directed I/O does. There have been multiple discussions about it being time for the ASA carriage control legacy to be removed, the leading space being the primary remnant; but some platforms still support the ASA control and it would affect a lot of existing code (but if you are using list-directed output you left a lot of things up to the compilers that are at least as big as a leading space, so that is not a water-tight argument against dropping it).
But in general the asterisk is a convenience for constructing applications quickly; and production codes should use formatted output or risk unexpected text alignment or line breaks as well.
Some like to create a little module of favorite .formats.
I do not remember seeing an argument of any substance saying why the standard cannot
include saying when no format is present that the space prefix is not generated, as in
print, x, y, z, i, ...
that has been suggested multiple times and ignored just as many times! Also see
and related commands on some systems like asa and nasa.
PS:
Matching print is also an option. Just add â1xâ at the beginning of all your Formats. A related question that comes up is why do so many Fortran libraries, particularly older ones
always use FORMAT statements that skip the first column of output? That is so they would work with the old ASA line printers. But sometimes new code does that as well; often just to be the same as the list-directed print statement!