Sage-Code Laboratory

Fortran Syntax

Fortran syntax is original. This language was invented and it inspire other languages with ideas and concepts created by visionary gods of computuer programming. Fortran has set the standard for structured and imperative programming.

Most languages these days are inspired from C, but Fortran was designed before C. It has started a different family of programming languages that use symilar keywords and concepts. Fortran is not a curly bracket language.

Define Program

In Fortran, you have to declare the main program using a special keyword that is "program". This can be the only block in the application but is mandatory.

syntax pattern

program name_of_program
  ! variable declarations 
  ! program statements 
end program

Program Name

Notes: You can see the program has a name. Most of the time this name is the same as the file name that is containing the main program. Unfortunately the compiler do not check this and it could be a readability issue. We recommend to use the same name.

Name of program can start with any letter and can contain letters and numbers and underscore. We think the program name should be short but is up to you to give it a significant name. You can name your program "main".

Line Comments

In Fortran the comments start with "!" and can be used at beginning of line or at end of line for each statement. Is a good engineering practice to comment your code properly.


Indentation is an equal number of spaces used to align first letter in a statement. Nested statements are indented with equal number of spaces or tabs. We recommand 4 spaces but you can use 2 spaces also. It is a convention you need to establish for each project.

Fortran 77 use to be space sensitive due to punch cards, but Fortran 90 is free form language and is not used with punch carts. The indentation is optional now but recommended. If you make indentation errors, the program will probably compile but is going to be harder to read.

What is a statement?

Fortran is a statement oriented language. Every line of code can represent an individual statement. Some statements can include other statements that are nested. This is called a block of code or block statement.

A statement usually start with a keyword and uses one or more expressions. Expressions can contain identifiers, operators, data-literals, function calls and delimiters. An expression that is not used in a statement does not make sense in Fortran.

continuation symbol

A statement can be extended on multiple lines of code. You can use continuation operator & that connect the lines. Operator & is not part of the statement and is ignored by the compiler.

Multiple Statements

You can have many statements on a single line. In this case you can separate the statement with semicolumn ";". Sometimes programmers add semicolumn to indicate "no statement" where you should have one.

Source Code

Source code is usually stored in a file. This has extension "f" or "f90" or "f95". Is depending on syntax version and compiler you are using. I recommand to use only "f95" version for this course.

The source code can represent a program or a program module. You can compile the source code into an executable object file. After you create several object files, you can link them together into an executable file.

Every source file can contain: declarations, statements or subprograms. Usually the main program is at the bottom in a source file. You can use only one main program for an application.

Hello World

Let's make the first example. This example is a little bit simplified to sho you how everything works together. In the console we compile the program and then we run it.

! main program
program main
  print  * , "Hello " // get_name()
  function get_name() result(name)
     character (len=40) :: name
     character (len=20) :: name1, name2
     print *, 'Who are you? :'
     read *, name1, name2
     name = trim(name1) // " " // &
  end function
end program
~>gfortran ./main.f95 -o main
 Who are you? :
Elucian Moise
 Hello Elucian Moise               

Note: Code examples are available on GitHub. This is an open source project. We run examples online on Some of the examples will be available for you to modify, compile and run.

Homework: Log-in to, and create your won example. Do not use copy paste. Instead, try to type the code line by line. You will get use with the syntax this way. I hope it works!


Fotran has a long hystory of modifications. To be brief, we mention the older major versions: Fortran 66, Fortran 77. The new version are Fortran90, Fortran 95, Fortran 2003, Fortran 2008, Fortran 2018. In the future we will have Fortran 2023 and beyound. Important changes:

Fortran 90 features

ISO/IEC standard 1539:1991 in 1991 and an ANSI Standard in 1992. In addition to changing the official spelling from FORTRAN to Fortran, this major revision added many new features to reflect the significant changes in programming practice that had evolved since the 1978 standard:

Fortran 95 extension

Fortran 95 introduce "High Performance Fortran". Is out of scope for this tutorial to present these features. You can read it yourself after you finish the fundamentals.

Deprecated features

It is good to know what deprecated features are yet presented in code or tutorials and may not work for your compiler. Do not try to use these features. If you do you will get an error: "deleted feature..." so you know you will not use it.

File Endings

Historically, the file endings of Fortran source files are .f and .for. These are associated with Fortran < 90, and therefore with fixed format. Even modern compilers assume fixed format for them, unless the free format command-line flag is used.

For this reason, file endings should be used that represent modern Fortran versions. We can either set endings that correspond with the actual language version (.f03 for 2003, .f08 for 2008, .f18 for 2018, and so on) or simply use .f90 as a default to indicate modern Fortran, with the latter being recommended. Be aware that you must set the file ending to either .f90 or .f95 to be recognised by CMake.

Read next: Data Types