Fortran Basics: A Beginner's Guide to Mastering Fortran
Fortran, short for "Formula Translation," is one of the oldest and most powerful programming languages still in use today. Developed in the 1950s, it was specifically designed for scientific and engineering calculations, and it remains a staple in these fields. Whether you're new to programming or an experienced coder looking to expand your knowledge, understanding Fortran basics is a valuable skill. In this article, we will dive into the fundamental concepts of Fortran and provide examples to help you get started.
What is Fortran?
Fortran is a high-level programming language that was created by IBM in the 1950s, primarily to provide an efficient way of solving mathematical and scientific problems. Over the decades, Fortran has evolved, and today it has several modern versions, such as Fortran 77, Fortran 90, Fortran 95, Fortran 2003, and Fortran 2008. Each new version of Fortran added more features, but the language's primary focus—scientific and engineering calculations—remained the same.
Fortran is known for its powerful numerical computation capabilities, and many scientific applications, such as weather forecasting, physics simulations, and engineering design, still rely on it. Understanding Fortran basics is the first step to unlocking its power and using it to solve complex problems. So let’s explore the essentials!
Fortran Syntax: The Building Blocks
Before you dive into coding with Fortran, it’s important to understand the basic syntax that defines the structure of the language. Let’s break down some of the key elements of Fortran syntax:
1. Comments
In Fortran, comments are written using the exclamation mark symbol (!). Anything after the exclamation mark on a line is ignored by the compiler and is considered a comment. Comments are used to explain the code and make it easier to understand.
! This is a comment in Fortran PRINT *, 'Hello, World!' ! This line prints text
2. Variables and Data Types
Fortran supports a wide range of data types, including integers, real numbers, complex numbers, and characters. Each variable in Fortran must be declared with a specific data type. Some common data types include:
- INTEGER: For whole numbers (e.g., 1, -5, 100).
- REAL: For floating-point numbers (e.g., 3.14, -0.5, 2.718).
- COMPLEX: For complex numbers, which consist of a real and imaginary part (e.g., 3.0 + 2.0i).
- CHARACTER: For strings of characters (e.g., 'Hello', 'Fortran').
Variables are declared at the beginning of a program or block of code using the INTEGER, REAL, COMPLEX, and CHARACTER keywords. For example:
INTEGER :: number REAL :: pi CHARACTER :: name
3. Assigning Values to Variables
Once you declare your variables, you can assign values to them using the = operator. For example:
number = 5 pi = 3.14159 name = 'Fortran'
4. Print Statements
In Fortran, the PRINT statement is used to display output to the console. For example:
PRINT *, 'Hello, World!'
The PRINT *, statement displays the text "Hello, World!" on the screen. The asterisk (*) means that the output is printed with the default formatting.
Control Structures: If-Else, Loops
Fortran provides several control structures that help you make decisions and repeat code execution. These include IF statements, loops, and more. Let’s go through them:
1. If-Else Statement
The IF statement is used to make decisions based on conditions. If the condition is true, a block of code is executed. If it’s false, you can provide an optional ELSE block to execute alternative code. Here’s an example:
IF (number > 0) THEN
PRINT *, 'Number is positive.'
ELSE
PRINT *, 'Number is not positive.'
END IF
2. Do Loops
In Fortran, DO loops allow you to repeat a block of code a specific number of times or while a condition is true. For example:
DO i = 1, 5
PRINT *, 'i =', i
END DO
This loop will print the values of i from 1 to 5. You can also use loops to perform calculations, iterate over arrays, and much more.
Working with Arrays in Fortran
Arrays are an essential feature in Fortran, especially when working with large datasets. An array is a collection of variables of the same type, but with different indices. For example:
REAL :: array(5) array(1) = 1.0 array(2) = 2.0 array(3) = 3.0 array(4) = 4.0 array(5) = 5.0
You can loop over arrays using DO loops to process or manipulate their elements. For instance, here’s how to print each element of the array:
DO i = 1, 5
PRINT *, 'array(', i, ') =', array(i)
END DO
Fortran Functions and Subroutines
Fortran allows you to create reusable blocks of code using FUNCTION and SUBROUTINE. These are essential for organizing your code and making it more modular. Here’s an example of a simple function that returns the square of a number:
FUNCTION square(x)
REAL :: square
REAL, INTENT(IN) :: x
square = x * x
END FUNCTION square
To call this function in your program:
REAL :: result result = square(5.0) PRINT *, 'The square of 5.0 is ', result
Common Fortran Examples
Let’s walk through a simple Fortran program that uses some of the basics we’ve learned. This program calculates the factorial of a number:
PROGRAM Factorial
INTEGER :: n, result, i
PRINT *, 'Enter a number:'
READ *, n
result = 1
DO i = 1, n
result = result * i
END DO
PRINT *, 'The factorial of', n, 'is', result
END PROGRAM Factorial
In this program, we read an integer n from the user, calculate its factorial using a loop, and then print the result.
Conclusion: Getting Started with Fortran
Fortran remains one of the most powerful and widely used languages in scientific computing. Understanding Fortran basics opens up a world of possibilities, whether you're working in academia, industry, or personal projects. With its simple syntax, efficient computation abilities, and continued evolution, Fortran is an excellent choice for tackling complex numerical problems.
In this article, we’ve covered some essential Fortran concepts, such as data types, variables, control structures, arrays, and functions. By practicing these basics, you’ll be well on your way to mastering Fortran and using it to solve a wide range of challenges. Happy coding!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!