Variables and Data Types in Fortran

Fortran, which stands for Formula Translation, is one of the oldest high-level programming languages, primarily designed for numerical and scientific computing. One of the fundamental concepts in Fortran programming is the use of variables and data types. Proper understanding and usage of variables and data types are crucial for writing efficient and error-free programs. This post will provide an in-depth explanation of variables, data types, assigning values, constants, and examples in Fortran.

1. Introduction to Variables

In Fortran, a variable is a named memory location that stores data that can be manipulated during program execution. Variables have specific data types that determine what kind of data they can hold, such as integers, real numbers, or text.

Declaring variables properly is essential because it informs the compiler about the kind of data the variable will hold, which affects memory allocation and operations performed on the variable.


2. Variable Declaration

Variable declaration is the process of defining the variable name and specifying its data type. In modern Fortran (Fortran 90 and later), the :: notation is used for declaration. The general syntax for variable declaration is:

data_type :: variable_name

For example:

integer :: i
real :: x
double precision :: y
character(len=20) :: name

Here, i is declared as an integer, x as a real number, y as double precision, and name as a character string with a maximum length of 20 characters.


3. Data Types in Fortran

Fortran supports several data types. The most commonly used are integer, real, double precision, and character.

3.1 Integer

The integer data type is used to store whole numbers. These numbers do not contain fractional parts.

Declaration:

integer :: a, b, c

Assigning Values:

a = 5
b = 10
c = a + b

Example Program:

program integer_example
  integer :: i, j, sum
  i = 15
  j = 25
  sum = i + j
  print *, "i =", i, "j =", j, "sum =", sum
end program integer_example

Explanation:

  • i and j store integer values.
  • sum calculates the total of i and j.
  • The print * statement displays the values on the screen.

3.2 Real

The real data type is used to store floating-point numbers, i.e., numbers with fractional parts.

Declaration:

real :: x, y, z

Assigning Values:

x = 3.14
y = 2.71
z = x * y

Example Program:

program real_example
  real :: x, y, product
  x = 3.14
  y = 2.71
  product = x * y
  print *, "x =", x, "y =", y, "product =", product
end program real_example

Explanation:

  • x and y store real numbers.
  • product stores the multiplication result.
  • Real numbers are typically used in scientific calculations.

3.3 Double Precision

The double precision data type is used for floating-point numbers that require higher accuracy. It allocates more memory than the standard real type.

Declaration:

double precision :: a, b, result

Assigning Values:

a = 3.141592653589793
b = 2.718281828459045
result = a + b

Example Program:

program double_precision_example
  double precision :: pi, e, sum
  pi = 3.141592653589793
  e = 2.718281828459045
  sum = pi + e
  print *, "pi =", pi, "e =", e, "sum =", sum
end program double_precision_example

Explanation:

  • Double precision is essential for high-accuracy calculations in scientific and engineering applications.
  • Operations on double precision variables maintain higher precision compared to real numbers.

3.4 Character

The character data type is used to store text strings. It is defined with a length, which specifies the maximum number of characters it can hold.

Declaration:

character(len=20) :: name

Assigning Values:

name = "John Doe"

Example Program:

program character_example
  character(len=20) :: name
  name = "John Doe"
  print *, "Name =", name
end program character_example

Explanation:

  • The len parameter defines the maximum number of characters in the string.
  • Character variables can be used to store and manipulate textual information.

4. Assigning Values to Variables

Values can be assigned to variables either during declaration or after declaration.

Assigning During Declaration:

integer :: i = 10
real :: x = 3.14

Assigning After Declaration:

integer :: i
real :: x
i = 10
x = 3.14

Both methods are valid, but assigning during declaration can make the code cleaner and easier to read.


5. Constants Using Parameter

In Fortran, constants are values that do not change during program execution. The parameter keyword is used to declare constants.

Syntax:

data_type, parameter :: constant_name = value

Example:

integer, parameter :: MAX = 100
real, parameter :: PI = 3.141592

Explanation:

  • MAX is a constant integer with a value of 100.
  • PI is a constant real number representing the value of π.
  • Constants help in writing code that is easier to maintain and less error-prone.

Program Example Using Constants:

program constants_example
  integer, parameter :: MAX = 100
  real, parameter :: PI = 3.141592
  integer :: i
  real :: area, radius

  radius = 5.0
  area = PI * radius**2
  print *, "MAX =", MAX
  print *, "Area of circle with radius", radius, "is", area
end program constants_example

6. Rules for Naming Variables

When declaring variables, certain rules must be followed:

  1. Variable names must start with a letter.
  2. Only letters, numbers, and underscores (_) are allowed.
  3. Variable names cannot exceed 31 characters in Fortran 90 and later.
  4. Avoid using Fortran keywords as variable names.
  5. Choose meaningful names to make the code readable.

Example of Valid Variable Names:

integer :: age, total_marks, radius_of_circle
real :: height, weight

Example of Invalid Variable Names:

integer :: 1number, total-marks, print

7. Implicit Typing

In Fortran, variables can be implicitly typed based on their first letter, but this practice is not recommended because it can lead to errors.

  • Variables starting with i through n are implicitly integers.
  • Variables starting with any other letter are implicitly real.

Example:

implicit none
integer :: i
real :: x
i = 10
x = 2.5

Explanation:

  • Using implicit none forces the programmer to declare all variables explicitly, reducing errors and improving code clarity.

8. Initialization and Default Values

Variables in Fortran can be initialized with default values during declaration. Uninitialized variables may contain random values, leading to unpredictable results.

Example:

integer :: i = 0
real :: x = 1.0
character(len=10) :: name = "Unknown"

Explanation:

  • Initializing variables ensures that they have a known value before any operations.

9. Summary

  • Variables are memory locations used to store data.
  • Fortran supports several data types: integer, real, double precision, and character.
  • Values can be assigned during or after declaration.
  • Constants are declared using the parameter keyword.
  • Always use meaningful variable names and declare variables explicitly with implicit none.
  • Proper understanding of variables and data types ensures accuracy, readability, and maintainability of programs.

10. Complete Example: Using All Data Types and Constants

program variables_and_constants
  implicit none
  integer :: i, j, sum
  real :: x, y, product
  double precision :: pi, e, sum_dp
  character(len=20) :: name
  integer, parameter :: MAX = 100
  real, parameter :: PI = 3.141592

  ! Assigning values to variables
  i = 10
  j = 20
  sum = i + j

  x = 3.0
  y = 4.0
  product = x * y

  pi = 3.141592653589793
  e = 2.718281828459045
  sum_dp = pi + e

  name = "Alice"

  ! Displaying values
  print *, "Integer values: i =", i, "j =", j, "sum =", sum
  print *, "Real values: x =", x, "y =", y, "product =", product
  print *, "Double precision values: pi =", pi, "e =", e, "sum_dp =", sum_dp
  print *, "Character value: name =", name
  print *, "Constant values: MAX =", MAX, "PI =", PI
end program variables_and_constants

Explanation:

  • This example demonstrates all fundamental data types in Fortran.
  • It uses constants to ensure fixed values.
  • The program prints results for integers, real numbers, double precision numbers, and character strings.

Comments

Leave a Reply

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