Assumed-shape arguments

A procedure that takes an array usually does not care how many elements it has: it wants the rank, the values, and the size when it needs to loop. a(:) in the dummy argument list says exactly that, and size(a), shape(a) and the bounds are there inside. This lesson passes arrays and sections to such a procedure and reads the shape the caller sent.

An assumed-shape dummy

real, intent(in) :: a(:) is a rank-one array of unknown extent: the number of elements comes from the actual argument at every call. A rank-two dummy adds a colon per dimension, a(:, :), and its shape is whatever the caller passed.

The file assumed.f90 puts the two procedures in a module, which is the usual place: a module gives every caller an explicit interface for free, as the program units lesson said.

module shapes implicit none contains subroutine show1(a, name) real, intent(in) :: a(:) character(len=*), intent(in) :: name print *, trim(name), ": size =", size(a), " sum =", sum(a) end subroutine show1 subroutine show2(m, name) real, intent(in) :: m(:, :) character(len=*), intent(in) :: name print *, trim(name), ": shape =", shape(m), " sum =", sum(m) end subroutine show2 end module shapes program assumed_demo use shapes, only: show1, show2 implicit none real :: x(5), m(3, 4) integer :: i x = [1.0, 2.0, 3.0, 4.0, 5.0] call show1(x, "x") call show1(x(2:4), "x(2:4)") m = reshape([(real(i), i = 1, 12)], [3, 4]) call show2(m, "m") end program assumed_demo

gfortran -Wall -Wextra -std=f2018 assumed.f90 -o assumed
./assumed

x: size = 5 sum = 15.0000000
x(2:4): size = 3 sum = 9.00000000
m: shape = 3 4 sum = 78.0000000

The module and the program are one file here, and gfortran compiles the module first because the program uses it: use makes the interface part of the caller, and the two units are built in one call.

A section as an argument

call show1(x(2:4), "x(2:4)") passes a section of three elements, and the procedure sees an array of three: the shape is part of what an assumed-shape argument carries. The dummy is a view of the section, and a copy is an implementation detail a program cannot observe, since intent(in) forbids writing through it anyway.

A section with a stride is allowed too, and this is one of the places where assumed shape earns its keep: the procedure does not need to know that the elements it reads are not neighbours in memory.

The lower bound inside

An assumed-shape dummy starts at one unless the declaration says otherwise, whatever the lower bound of the actual argument was. That is a deliberate simplification: the procedure indexes from one, and the caller may have a window that starts at minus two.

The file lower.f90 passes an array declared x(-2:2) to two procedures, the second of which asks for a lower bound of zero:

program lower_demo implicit none real :: x(-2:2) x = 10.0 call tell(x) call tell_zero(x) contains subroutine tell(a) real, intent(in) :: a(:) print *, "size :", size(a) print *, "lbound :", lbound(a) print *, "ubound :", ubound(a) print *, "a(1) :", a(1) end subroutine tell subroutine tell_zero(a) real, intent(in) :: a(0:) print *, "lbound :", lbound(a), " a(0) :", a(0) end subroutine tell_zero end program lower_demo

gfortran -Wall -Wextra -std=f2018 lower.f90 -o lower
./lower

size : 5
lbound : 1
ubound : 5
a(1) : 10.0000000
lbound : 0 a(0) : 10.0000000

Five elements arrived although the caller declared -2:2, and the first of them is a(1) in the first procedure and a(0) in the second. Only the declared lower bound changes; the data and the upper bound follow it. A procedure that must know the caller bounds asks for them as separate arguments.

The explicit interface

The compiler cannot build a call to an assumed-shape procedure from the call alone: it must know that the dummy is assumed shape, because the actual argument is passed as a descriptor and not as a bare address. That is why a(:) needs an explicit interface, which a module supplies and an interface block can supply. A plain external subroutine has none, and the compiler refuses the call.

The file noface.f90 puts the procedure after end program, so no module and no interface is in sight:

program noface implicit none integer :: a(4) a = [1, 2, 3, 4] call total(a) end program noface subroutine total(a) integer, intent(in) :: a(:) print *, sum(a) end subroutine total

gfortran -Wall -Wextra -std=f2018 noface.f90 -o noface

noface.f90:6:12:

6 | call total(a)
| 1
Error: Explicit interface required for 'total' at (1): assumed-shape argument

The error names the argument and the reason in one line, and the fix is not to change the call but to give the procedure the interface it needs: put it in a module, add an interface block, or make it a contained procedure. An explicit-shape dummy such as a(n) does not need the interface, which is why old code is full of it; the next lesson is about that form.

Exercises

  1. Call show1 with x(1:5:2). What size and sum does it print?
  2. Add a procedure that takes a(:) and returns the mean, and call it with the whole array and with a section.
  3. Declare the dummy of tell as a(-2:). Which numbers does the output show now?
  4. Move total into a module and call it. Which line of the build output disappears?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a(:) as a dummy argument mean?

    Answer

    A rank-one array whose extent comes from the actual argument

    Assumed shape takes the extent from the argument at each call.

  2. What is size(a) inside a procedure with a(:) when the caller passes a five-element array?

    Answer

    5

    The descriptor carries the extent, so SIZE answers it.

  3. What lower bound does an assumed-shape dummy have by default?

    Answer

    One

    The dummy starts at one unless its declaration names another bound.

  4. What does this program print? module m implicit none contains subroutine s(a) integer, intent(in) :: a(:) print *, size(a), lbound(a) end subroutine s end module m program p use m implicit none integer :: x(-1:2) x = 0 call s(x) end program p

    Answer

    4 1

    The extent is four and the dummy starts at one.

  5. Why does an assumed-shape dummy need an explicit interface?

    Answer

    The argument is passed as a descriptor, so the caller must know the dummy is assumed shape

    Without the interface the compiler treats the argument as a bare address.

  6. What does gfortran report for a call to an external subroutine with an assumed-shape dummy?

    Answer

    Error: Explicit interface required for ... assumed-shape argument

    The call cannot be built without the interface.

  7. Which declaration gives a rank-two assumed-shape dummy?

    Answer

    m(:, :)

    One colon per dimension, with no numbers.

  8. Can a section such as x(2:4) be passed to an assumed-shape dummy?

    Answer

    Yes, the dummy sees an array of three elements

    The section carries its own extent and lower bound of one.

  9. What is the sum printed by show1(x(2:4), ...) for x = [1,2,3,4,5]?

    Answer

    9.0

    The section holds 2, 3 and 4, whose sum is 9.

  10. Which of these gives an assumed-shape procedure an explicit interface without an INTERFACE block?

    Answer

    Putting it in a module

    A module procedure carries its interface to every user of the module.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image