Fortran Programming Language

Fortran: A Basic Introduction

Fortran (short for Formula Translation) is a high-level programming language commonly used for scientific and engineering applications. It has been around for a long time and is especially well-suited for numerical and scientific computing. In this tutorial, we’ll cover some basic concepts to get you started with Fortran programming.

1. Setting Up Your Environment

Before you start writing code, you’ll need a Fortran compiler. Some popular compilers are GNU Fortran (gfortran), Intel Fortran Compiler, and IBM XL Fortran. We will use gfortran in this tutorial.

For Linux, open the terminal and run the following:

sudo apt-get update
sudo apt-get install gfortran

On MacOS, similarly, open the terminal and run this:

brew install gcc

For Windows, you can use the MinGW-w64 distribution, which includes gfortran.

  1. Download the MinGW-w64 installer.
  2. Run the installer and follow the installation instructions.
  3. During the installation, make sure to select the options for installing Fortran. Look for an option similar to “Fortran Compiler” or “gfortran.”

That done, you’ll also need a code editor. Personally I use VS Code but others like Sublime work just fine.

2. Your First Fortran Program

Let’s begin with a simple “Hello, World!” program.

! This is a comment
program HelloWorld
  ! Main program starts here
  print *, "Hello, World!"
end program HelloWorld

The ! symbol is used for comments, and the print * statement outputs text to the console. program and end program serves to declare the beginning and end of your program, and it’s name.

Save this code in a file with a “.f90” or “.f” extension, for example, hello.f90.

3. Compiling and Running

After saving your Fortran program, open a terminal or command prompt, navigate to the directory containing your code, and compile it using the Fortran compiler. Open your system’s terminal, navigate to the folder where you saved your program and do

gfortran hello.f90 -o hello

This command tells the compiler to take the source file hello.f90 and produce an executable named hello. Now you can run your program:

./hello

You should see the output “Hello, World!” on your console.

4. Variables and Data Types

Fortran supports different data types, such as integers, real numbers(float), double precision, and characters. Declare variables with the integer, real, or character keywords.

program VariablesExample
  integer :: age
  real, parameter             :: single 
  double precision, parameter :: double

  character(len=20) :: name

  age = 25
  single = 1.12345678901234567890
  double = 1.12345678901234567890d0
  name = "John Doe"

  print *, "Name:", name
  print *, single
  print *, double
  print *, "Height:", height
end program VariablesExample

Here, name will store and print “John Doe”, age “25”, the real variable(single) will print and store “1.12345684”, and double “1.1234567890123457”.

5. Control Structures

Fortran supports standard control structures like if, do, and do while for making decisions and repeating tasks.

program ControlStructuresExample
  integer :: num

  print *, "Enter a number: "
  read *, num

  if (num > 0) then
    print *, "The number is positive."
  else if (num < 0) then
    print *, "The number is negative."
  else
    print *, "The number is zero."
  end if
end program ControlStructuresExample

6. Arrays

Arrays in Fortran are used to store multiple values of the same data type.

program ArraysExample
  integer :: numbers(5)
  integer :: i

  do i = 1, 5
    numbers(i) = i * 2
  end do

  do i = 1, 5
    print *, "Element", i, ":", numbers(i)
  end do
end program ArraysExample

7. Subroutines

A subroutine in Fortran is a collection of statements that can be called from other parts of the program. Subroutines are defined using the subroutine keyword. They can have input parameters, output parameters, or both.

subroutine MySubroutine(x, y)
  ! Input parameters
  real, intent(in) :: x, y

  ! Code to perform a task

  ! No return statement; control returns to the calling program
end subroutine MySubroutine

To call a subroutine from the main program:

program MainProgram
  real :: a, b

  a = 5.0
  b = 3.0

  ! Call the subroutine
  call MySubroutine(a, b)

  ! Continue with the main program
end program MainProgram

8. Functions

A function in Fortran is similar to a subroutine, but it returns a value. Functions are defined using the function keyword.

function MyFunction(x, y) result(result_value)
  ! Input parameters
  real, intent(in) :: x, y

  ! Output parameter
  real :: result_value

  ! Code to perform a task and compute the result

  ! Set the result value
  result_value = x + y
end function MyFunction

To call a function from the main program:

program MainProgram
  real :: a, b, result

  a = 5.0
  b = 3.0

  ! Call the function and store the result
  result = MyFunction(a, b)

  ! Continue with the main program
end program MainProgram

As you become more familiar with the language, you can explore advanced topics like procedures, modules, and advanced array manipulation. Fortran has a rich set of features for scientific and numerical computing, making it a powerful language for various applications, and extremely popular for super computers.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *