Fortran
Official website
Fortran isn’t managed by a single centralized organization with a dedicated “official website” the way some languages are. Instead, the language standards for Fortran are developed and maintained by the ISO/IEC JTC1/SC22/WG5 working group. For information directly from the standards committee, you can visit the WG5 website:
https://wg5-fortran.org/
This site provides details on the latest standards efforts, meeting information, and relevant documents. Additionally, for historical and general resources on Fortran you might also find the Fortran Wiki and pages hosted by various Fortran users’ groups useful.
Static typing
Yes, Fortran is considered a statically typed language. This means that the type of every variable is determined at compile time, not at runtime. In Fortran, you typically declare the type of each variable explicitly. For example:
INTEGER :: i
REAL :: x
DOUBLE PRECISION :: y
Unless you specify otherwise, Fortran also applies implicit typing rules (based on variable names), which means that variables starting with certain letters (by default I-N) are assumed to be of type INTEGER and others as REAL. However, it’s a common best practice to disable this implicit behavior by including the statement “IMPLICIT NONE” at the beginning of your program or subroutine. This requires you to explicitly declare every variable, further reinforcing the static type system:
PROGRAM Example
IMPLICIT NONE
INTEGER :: count
REAL :: value
count = 5
value = 3.14
PRINT *, "Count = ", count, " Value = ", value
END PROGRAM Example
In summary, Fortran’s type system is static because types are known and checked at compile time rather than being determined dynamically during execution.
Example code
Below is an example implementation in Fortran 90 that defines a binary search tree (BST) data structure and includes an algorithm (a recursive function) to find whether a given integer value exists in the BST. In this example, the BST is built using dynamic pointers and a recursive search procedure. You can compile this code with a Fortran compiler (for example, gfortran).
program bst_search
implicit none
! Define a pointer-based binary search tree node as a derived type.
type :: BSTNode
integer :: value = -1
type(BSTNode), pointer :: left => null()
type(BSTNode), pointer :: right => null()
end type BSTNode
type(BSTNode), pointer :: root => null()
! Local variables.
integer :: value_to_find, search_result
integer, dimension(7) :: data = [15, 10, 20, 8, 12, 17, 25]
integer :: i
! Insert all data items into BST.
do i = 1, size(data)
call insert(root, data(i))
end do
! Ask user for input value to search.
print *, "Enter an integer value to find:"
read(*,*) value_to_find
! Search for the value in the BST.
search_result = search(root, value_to_find)
if (search_result == 1) then
print *, "Found the value ", value_to_find
else
print *, "Value ", value_to_find, " not found in the tree."
end if
contains
!---------------------------------------------------------------------------
! Recursive procedure to insert a new value into the BST.
subroutine insert(node, new_value)
type(BSTNode), pointer :: node
integer, intent(in) :: new_value
type(BSTNode), pointer :: newNode
if (.not. associated(node)) then
! If current node pointer is not associated, allocate a new node.
allocate(node)
node%value = new_value
node%left => null()
node%right => null()
else if (new_value < node%value) then
call insert(node%left, new_value)
else if (new_value > node%value) then
call insert(node%right, new_value)
else
! Duplicate value encountered; here we do nothing.
end if
end subroutine insert
!---------------------------------------------------------------------------
! Recursive function to search for a value in the BST.
! Returns 1 if found, 0 if not found.
integer function search(node, key) result(found)
type(BSTNode), pointer :: node
integer, intent(in) :: key
if (.not. associated(node)) then
found = 0
else if (node%value == key) then
found = 1
else if (key < node%value) then
found = search(node%left, key)
else
found = search(node%right, key)
end if
end function search
end program bst_search
Explanation
-
BSTNode Type Definition:
The derived typeBSTNodecontains an integer value and two pointers (leftandright) pointing to its child nodes. -
Insertion Subroutine (
insert):
In theinsertsubroutine, if the pointer is not associated (i.e., the spot is empty), a new node is allocated. Otherwise, the procedure decides to insert either to the left or the right based on the BST property. - Search Function (
search):
Thesearchfunction is a recursive function that checks the current node:- If the node is not associated, the search has reached a dead end, and the function returns 0 (not found).
- If the node’s value matches the search key, the function returns 1 (found).
- If the key is less than the current node’s value, the function recurses into the left subtree; otherwise, into the right subtree.
- Main Program:
A sample BST is built with an array of integers. The code then reads a value from the user, searches for it in the BST, and prints whether the value was found.
Compile and run this code, then follow the prompt to enter the integer to search for.
This implementation demonstrates how to create and manipulate a BST in Fortran and how to perform a binary search within it.