Whole arrays or sections of arrays can be manipulated using array syntax. Examples:

real, dimension(5,10) :: x, y
real, dimension(5)    :: z

x = y                     ! copies whole array y into array x

x(1:5,1:10) = y(1:5,1:10) ! same but with explicit array bounds

z = x(1:5,2)              ! copies the second column of array x
                          ! into the vector z

z = x(:,2)                ! same copy with a colon (:) as a
                          ! placeholder for all elements of a row

z = x(:,2) + 1.0          ! same but adding 1.0 to each element

x(1:4,2:3) = y(2:5,8:9)   ! more complicated copying with
                          ! index shifts
Fortran

Array intrinsic functions can be passed whole arrays as arguments. Examples:

real, dimension(5,10) :: x, y
real, dimension(5)    :: z
integer               :: m
integer, dimension(2) :: n

a = maxval(x)     ! maximum value of the array x

m = maxloc(z)     ! array index that holds the largest value
                  ! in the array z

n = maxloc(x)     ! the two array indices of the array element
                  ! with the largest value. The variable has to
                  ! be a vector with a length that matches
                  ! the number of dimensions of the
                  ! argument (x).
Fortran

Other functions are: minval, minloc, etc.

Arrays and scalars can be initialized when they are declared. Fortran 90's (/.../) syntax is used for assigning the contents of an array during declaration. This array syntax is valid in executable statements as well. Alternatively, a variable may be initialized using a separate data statement, which has its own special syntax for defining the contents of an array. Typically, data statements are found in a block immediately after the declaration part of a program unit, but they can appear in the executable part of the code, too. Examples:

real                :: c = 5  ! Initialization without a data statement
real, dimension(5)  :: x, y   ! Will be initialized with data statements
real, allocatable   :: a(:)   ! Can't yet initalize

real, dimension(5)  :: z = (/ 1., 2., 4., 8., 16. /)
                                       ! Initialize vector

data x / 1., 2., 3.14, -17., 0. /      ! Initialization with
                                       ! a data statement

data x(1) / 7. /                       ! First element of x
                                       ! is set to 7.

data y(1:3) / 3*5. /                   ! Three elements of y
                                       ! are set to 5.

allocate(a(5))                         ! Allocate a; initialize
a = (/ 1., 1., 2., 3., 5. /)           ! using array notation.
Fortran
 
© 2025  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Access Statement
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)