In this tutorial, I’ll introduce you to the most basic things you should know about FORTRAN:
– How to write a quick Hello World Script
– How to compile it
– How to launch it ;-)
All the tutorials will be on Ubuntu 16.04 that you can download and install freely on a second partition or a virtual machine if you like:
You don’t need to have Ubuntu though to compile fortran of course!
There are some solutions for windows and mac
Just search “compile FORTRAN on windows” and you will find quite a few ;-)
1- Why studying Fortran?
I decided to make a few videos about Fortran and that’s why I’m talking about that right now on the blog
Let me explain first why I’m doing that because Fortran is a very old language…
So why would you like to study Fortran actually?
if this is your first language and you are just getting into programmation and you want to make some simple programs I don’t recommend you to go into Fortran so just leave this video and instead try to learn Python
I’ll have also some videos about Python too on the blog soon… ;-)
OK…still want to learn Fortran?… you must have some good reasons then ;-)
For me, it was mainly because I was looking into the source code of Code_Aster, and I realised that so much of it is written in FORTRAN…. and I just don’t get it, haha
… but after spending some time on understanding the logic, you will find it is not as complex as you may think!
So if like me, you want to understand the algorithms behind the source code of some dinosaur(but still extremely powerful ;-) ) software
…if you want to be able to to add your own functions to compile the source code and all the these things, then you will need to have a basic knowledge of FORTRAN
That’s what this series of videos is about and that’s what I’ll teach here!
I created them with the purpose to get you into the very basics of Fortran and I hope it will really be useful for you to understand the source code of other programs
okay so without any more talking let’s start right now
2- Creating the folder and file of your program
so let’s go into my desktop and what I’ll do is that I’ll create a very
simple hello world program to show you how to write your first Fortran program
and how it works to compile it so let’s make a new directory called Fortran
The new Folder now appears on my desktop
(By the way I'm on Linux Ubuntu 16.04 but you can also use and do Fortran on other systems... for the compilation it will be a
bit different... just search "how to compile fortran" on google and you'll find a lot of stuff)
So I have my Fortran file now...
Let's get into it and let's open a new text file. Let's call
it "helloworld" and give it an extension F95.
so I'm using here the convention of Fortran 95 there are different kind of
fortran files (fortran 77, fortran 90
Fortran 95, etc...)
The oldest is (I think) FORTRAN 77 which is very old kind of format which still use fixed format source code and has a lot of annoying stuff inside so if you really want to do that well your you
can do it but the the best is to start with fortran 95...
okay so now let's open my new file
emacs helloworld.f95 (this commands uses the text editor program emacs, but you can use any other you have installed... like "nano" or "vim" for example...)
I'm using Emacs text editor but you can use gedit, nano or you can use any kind of text editor... I'm just discovering Emacs those days and I find it's really fun so I'm just you know using that
(just try Esc-X tetris and you'll understand why I say emacs is fun, haha... so many pearl hidden inside it)
Anyhoo... Let's go into our first program
3- Writing the program
So I open my file so it starts always with a common "program" command and the name of your program
My program will be called "helloworld" so I'm just entering hello world and then you have to close the program with an "end program"
#Here goes your code
end program helloworld
Now you have you have you have to write something in between right...
I just want to create a basic function which will print "hello world"
to the screen so here what we have to write:
print * , "Hello World!"
end program helloworld
In Fortran, you put a small star after the print which will be replaced after by the string that you want to print "Hello World", provided after the comma.
Let's save (Ctrl- X + S with emacs) and now it's ready so it's really quite simple
4- Compiling your Fortran program
Now... how do i execute and actually get "Hello World!" printed?
I will have to compile with gfortran to do that (gfortran need to be installed of course)
So that's that's how it works:
Now, open a new terminal window and I will use gfortran program to compile my fortran file with the following command:
cd Fortran (if you are not inside the folder already)
gfortran helloworld.f95 -o helloworld (this is the command to compile)
Note that gfortran is also a program that need to be installed on
your computer so if you don't have it you need to install it
(I think generally in Linux distribution, you always have gfortran installed by default...)
gfortran helloworld.f95 -o helloworld
The option "-o helloworld" that you see here is to tell the compiler that you want to name you executable file "helloworld"
After inputting this command, you will see that the executable "helloworld" has been generated
5- Executing the program
To launch and execute it, just enter this command in your terminal:
I have to execute it from the terminal common because now it's a program without any graphical interface so the command prompt displays what you want to see.
This is what you should see now:
So that's really a very very simple program but that show you how to do this kind of hello world program
now if you're not on Linux Ubuntu your windows
or something you'll have to find a way to compile a Fortran program with a specific windows compiler...
That's all for this tutorial!
In the next video we'll see a bit more about how to add things into the file like variables, how to declare them, how to read a variable from terminal, etc...