Difference between revisions of "Fortran1"

From SourceWiki
Jump to navigation Jump to search
Line 15: Line 15:
 
</pre>
 
</pre>
  
and type:
+
compile it by typing:
  
 
<pre>
 
<pre>
Line 21: Line 21:
 
</pre>
 
</pre>
  
Now type:
+
and run it by typing:
  
 
<pre>
 
<pre>
Line 49: Line 49:
 
#some comment lines, giving us a helpful narrative
 
#some comment lines, giving us a helpful narrative
 
#the start of the '''main program unit'''
 
#the start of the '''main program unit'''
#the '''implicit none''' statement (more of this in a moment)
+
#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
 
#a '''write''' statement, printing our greeting to the screen
 
#and last, but not least, the end of the main program.
 
#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 [[source.ggy.bris.ac.uk/wiki/intro-to-make 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 hell 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:
 +
 +
<pre>
 +
svn revert
 +
</pre>
 +
 +
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==
 
==Baskets and the Types of Things==
 +
 +
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 said "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.
  
 
==If, Do, Select and Other Ways to Control the Flow==
 
==If, Do, Select and Other Ways to Control the Flow==

Revision as of 21:53, 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:

  1. some comment lines, giving us a helpful narrative
  2. the start of the main program unit
  3. the implicit none statement (more of that in the next section, but suffice to say, every well dressed Fortran program should have one)
  4. a write statement, printing our greeting to the screen
  5. 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 source.ggy.bris.ac.uk/wiki/intro-to-make 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 hell 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

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 said "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.

If, Do, Select and Other Ways to Control the Flow

Not one, Many!

If Things get Hectic, Outsource!

You can Have That, and I'll Have This