HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to FortranVariablesOperatorsConditions (if)Loops (do)ArraysFunctions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
🔁 Condition & Loop
📋 Array
🧩 Functions & Subroutines
Fortran›Variables

Variables

integer, real, character types; declarations at the beginning of the code, and parameter constants.

Simple enough for a 5-year-old to understand

Variable declaration

Variables are declared at the BEGINNING of the program, before executable lines.

Example code
1
program h
2
implicit none
Good habit: forces you to declare every variable.
CodeGood habit: forces you to declare every variable.
3
integer :: age
Integer.
VariableInteger.
4
real :: height
Decimal.
VariableDecimal.
5
age = 36
6
height = 1.72
7
print *, age, height
36 1.72
Output / Input36 1.72
8
end program h
CodeVariableOutput / Input
💡 Hint: Declarations are gathered at the beginning of the code; you cannot define new variables in between.

About this topic

Variables in Fortran are defined with declaration statements, and declarations are gathered at the beginning of the program, before executable statements. This order is mandatory; declarations and statements cannot be mixed.

The most critical habit in the language is writing the directive that disables implicit typing at the beginning of every program. Without this directive, every undeclared name quietly becomes a variable, and typos are not caught at compile time.

The precision of numeric types in Fortran is specified with a kind parameter. If you want to perform double-precision calculations, you must write this explicitly; otherwise, the compiler default is single precision, which is insufficient for scientific calculations.

What you will learn

  • Declaration syntax
  • Using implicit none
  • Defining constants
  • Specifying precision
  • Assigning an initial value

Who is this for?

  • Beginners to Fortran
  • Those dealing with precision issues
  • Modernizing legacy code

When to use?

The precision parameter in scientific calculations should be defined once in a module and used everywhere; thus, the precision of the entire program can be changed with a single line of change.

Physical constants and dimension values should be defined as constants; the compiler embeds them into the code and prevents them from being accidentally modified.

Common mistakes

Wrong

program test
  implicit none
  real :: pi = 3.14159265358979
  real :: alan, yaricap
  yaricap = 2.0
  alan = pi * yaricap ** 2
  print *, alan
end program test

Numeric constants are single-precision by default; most of the decimal places you write are lost, and the result is limited to about seven significant digits. If you want double precision, you need to write both the variable and the constant with the precision parameter.

Correct

program test
  implicit none
  integer, parameter :: dp = kind(1.0d0)
  real(dp), parameter :: pi = 3.14159265358979_dp
  real(dp) :: alan, yaricap
  yaricap = 2.0_dp
  alan = pi * yaricap ** 2
  print *, alan
end program test

Wrong

program test
  implicit none
  integer :: toplam
  toplam = toplam + 10
  print *, toplam
end program test

Since the variable is not initialized, its initial value is undefined; some compilers zero it out, while others use whatever happens to be in memory. The result varies depending on the compiler and compilation options. Accumulators must always be explicitly reset to zero.

Correct

program test
  implicit none
  integer :: toplam
  toplam = 0
  toplam = toplam + 10
  print *, toplam
end program test

Wrong

program test
  implicit none
  integer :: i
  i = 5
  real :: x
  x = 2.0
  print *, i, x
end program test

The declaration line comes after an executable statement; Fortran does not accept this and results in a compilation error. All declarations must be gathered at the beginning of the program or subprogram, before any assignments.

Correct

program test
  implicit none
  integer :: i
  real :: x
  i = 5
  x = 2.0
  print *, i, x
end program test

Frequently asked questions

What is a precision parameter?

It is an integer value that determines how many bits will represent a numeric type. Writing the number of bits directly is not portable; instead, the parameter is derived using standard functions that query the desired precision, and that is used everywhere.

How is a constant defined?

The constant attribute is added to the declaration and the value is given on the same line. Assigning a value to such a variable later is a compilation error; it is the right tool for physical constants and array dimensions.

Is it safe to initialize a value in the declaration?

It is fine in the main program. In subprograms, an initial value given in the declaration turns the variable into one that retains its value between calls; if you expect it to reset on every call, you must perform the assignment in the body.

Summary

Declarations come before statements.

implicit none is written in every program.

Precision is specified with a type parameter.

Numeric constants also require a precision suffix.

Accumulators must be explicitly reset to zero.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code