Difference between revisions of "Fortran1"
m (Fortran1: Fortran basics moved to Fortran1) |
|||
Line 64: | Line 64: | ||
==Baskets and the Types of Things== | ==Baskets and the Types of Things== | ||
+ | |||
+ | As fun as "hell, world" was, let's spice things up a little. Let's introduce some variables. Fortran90 has several built-in, or '''intrinsic''', variables: | ||
+ | |||
+ | <pre> | ||
+ | character :: sex ! a letter e.g. 'm' or 'f' | ||
+ | character(len=12) :: name ! a string | ||
+ | logical :: wed ! married? | ||
+ | integer :: numBooks ! must be a whole number | ||
+ | real :: height ! e.g. 1.83 m (good include units in comment) | ||
+ | complex :: z ! real and imaginary parts | ||
+ | </pre> | ||
+ | |||
+ | These suffice for a great many programs. The above are all single entities. We'll meet arrays things in a couple of examples time. In ''Fortran2'', we'll also meet user-defined types. These allow us to group instances of intrinsic types together into a new kind of thing--a new type. User-defined types are the ''bees knees'' and can make programs much easier to work with. We'll leave the details to that later course, however. | ||
+ | |||
+ | The above snippet shows some variable '''declarations''', along with a helpful comment. It's good practice to comment your declarations as a programmer new to your code (or even yourself in a month or so's time) can have a hard time figuring out what is supposed to be stored in that variable named '''xbNew''', or '''z2'''. Oh, while we're at it, it's also good practice to give your variables meaningful names, even if they are long. Trust me, a bit more typing now, perhaps, but a lot less head-scratching later! | ||
+ | |||
+ | We can also give variables an initial value when we declare them: | ||
+ | |||
+ | <pre> | ||
+ | character :: nucleotide = 'A' ! DNA has A,C,G & T | ||
+ | character(len=50) :: infile = 'yourData.nc', outfile = "myData.nc" | ||
+ | logical :: initialised = .true. ! or .false. | ||
+ | real :: solConst = 1.37 ! Solar 'constant' in kW/m^2 | ||
+ | complex :: sqrtMinusOne = (-1.0, 0.0) ! sqrt(-1) | ||
+ | </pre> | ||
Now, about that mysterious '''intent none'''. Let me tell you a story. Once upon a time, the kings and queens of the garden of Fortran, being a generous and well meaning bunch, decided to save the programmers the bother of specifying the type of their variables. "Don't bother!", they said, "just be sure to give 'em" appropriate names, and well sort out the rest. "Thank you very much", said the programmers, and it was decreed that the names of reals should start with the letters i,j & k, integers with... Anyhow, this all seemed like a great wheeze and everybody was very happy. This lasted for a while, but after time, people forgot how to name things and it all got rather messy. Integers became reals, reals became ... and before they knew it, the programmers had '''BUGS''' all over the garden. Rubbish. The kings and queens conferred on the matter and they realised that they had made a grave error in the gift of implicit typing. However, they couldn't undo what they had done. Instead, they had to persuade the programmers to give it up voluntarily. "Anything, anything!", they pleaded "to get rid of these '''BUGS'''!", and so it passed that every good programmer agreed to put '''implicit none''' at the top of every program they write. | Now, about that mysterious '''intent none'''. Let me tell you a story. Once upon a time, the kings and queens of the garden of Fortran, being a generous and well meaning bunch, decided to save the programmers the bother of specifying the type of their variables. "Don't bother!", they said, "just be sure to give 'em" appropriate names, and well sort out the rest. "Thank you very much", said the programmers, and it was decreed that the names of reals should start with the letters i,j & k, integers with... Anyhow, this all seemed like a great wheeze and everybody was very happy. This lasted for a while, but after time, people forgot how to name things and it all got rather messy. Integers became reals, reals became ... and before they knew it, the programmers had '''BUGS''' all over the garden. Rubbish. The kings and queens conferred on the matter and they realised that they had made a grave error in the gift of implicit typing. However, they couldn't undo what they had done. Instead, they had to persuade the programmers to give it up voluntarily. "Anything, anything!", they pleaded "to get rid of these '''BUGS'''!", and so it passed that every good programmer agreed to put '''implicit none''' at the top of every program they write. |
Revision as of 22:58, 31 January 2008
Fortran1: The Basics
We'll forge our path through the verdant garden of Fortran90 using a number of examples. To get your copy of these examples, from the version control repository, login to your favourite linux machine (perhaps dylan), and type:
svn co http://source.ggy.bris.ac.uk/subversion/fortran1/trunk --username=guest ./fortran1
hello, world
Without further ado, and in-keeping with the most venerable of traditions, let's meet our first example--"hello, world":
cd fortran1/examples/example1
compile it by typing:
make
and run it by typing:
./hello_world.exe
Bingo! You've just compiled and run, perhaps your first, Fortran90 program. Hurrah! we're on our way:) Everybody whoop! Yeehah!
OK, OK...you'd better reign in your excitement. This is serious you know!:)
Enough of the magic, let's take a look inside the source code file. Open-up hello_world.f90, using cat, less, more or your favourite text editor, and you'll see:
! ! This is a comment line. ! Below is a simple 'hello, world' program written in Fortran90. ! It illustrates creating a main 'program' unit together ! with good habits, such as using 'implicit none' and comments. ! program hello_world implicit none write(*,*) "hello, world" end program hello_world
We have:
- some comment lines, giving us a helpful narrative
- the start of the main program unit
- the implicit none statement (more of that in the next section, but suffice to say, every well dressed Fortran program should have one)
- a write statement, printing our greeting to the screen
- and last, but not least, the end of the main program.
We'll be using make to compile all our example programs, so you won't have to worry about that side of things. (If you'd like to know more about make, you can take a look at our course on make, presented in a very similar style to this here excursion into Fortran90.)
This is all pretty straight forward, right? Open-up your text editor and try changing the greeting, just for the heck of it. Retype make and re-run it. We'll adopt a similar strategy for all the other examples we'll meet. If you ever want to get back to the original version of a program, just type:
svn revert
Although this has all been fairly painless, we have made a very significant step--we are now editing, compiling and running Fortran programs. All the rest is basically just details!:)
Baskets and the Types of Things
As fun as "hell, world" was, let's spice things up a little. Let's introduce some variables. Fortran90 has several built-in, or intrinsic, variables:
character :: sex ! a letter e.g. 'm' or 'f' character(len=12) :: name ! a string logical :: wed ! married? integer :: numBooks ! must be a whole number real :: height ! e.g. 1.83 m (good include units in comment) complex :: z ! real and imaginary parts
These suffice for a great many programs. The above are all single entities. We'll meet arrays things in a couple of examples time. In Fortran2, we'll also meet user-defined types. These allow us to group instances of intrinsic types together into a new kind of thing--a new type. User-defined types are the bees knees and can make programs much easier to work with. We'll leave the details to that later course, however.
The above snippet shows some variable declarations, along with a helpful comment. It's good practice to comment your declarations as a programmer new to your code (or even yourself in a month or so's time) can have a hard time figuring out what is supposed to be stored in that variable named xbNew, or z2. Oh, while we're at it, it's also good practice to give your variables meaningful names, even if they are long. Trust me, a bit more typing now, perhaps, but a lot less head-scratching later!
We can also give variables an initial value when we declare them:
character :: nucleotide = 'A' ! DNA has A,C,G & T character(len=50) :: infile = 'yourData.nc', outfile = "myData.nc" logical :: initialised = .true. ! or .false. real :: solConst = 1.37 ! Solar 'constant' in kW/m^2 complex :: sqrtMinusOne = (-1.0, 0.0) ! sqrt(-1)
Now, about that mysterious intent none. Let me tell you a story. Once upon a time, the kings and queens of the garden of Fortran, being a generous and well meaning bunch, decided to save the programmers the bother of specifying the type of their variables. "Don't bother!", they said, "just be sure to give 'em" appropriate names, and well sort out the rest. "Thank you very much", said the programmers, and it was decreed that the names of reals should start with the letters i,j & k, integers with... Anyhow, this all seemed like a great wheeze and everybody was very happy. This lasted for a while, but after time, people forgot how to name things and it all got rather messy. Integers became reals, reals became ... and before they knew it, the programmers had BUGS all over the garden. Rubbish. The kings and queens conferred on the matter and they realised that they had made a grave error in the gift of implicit typing. However, they couldn't undo what they had done. Instead, they had to persuade the programmers to give it up voluntarily. "Anything, anything!", they pleaded "to get rid of these BUGS!", and so it passed that every good programmer agreed to put implicit none at the top of every program they write.