David Hansen
David Hansen

Reputation: 521

Fortran, passing array using starting index only

In the program below, there are two methods presented for passing an array:

program main
    integer, dimension(4) :: x = [9, 8, 7, 6]
    call print_x(x(2:3))   ! Method 1
    call print_x(x(2))     ! Method 2
end program

subroutine print_x(x)
    integer, dimension(2), intent(in) :: x
    print *, x
end subroutine

Both methods produce the same result: the numbers 8 and 7 are printed. Personally, I would never code this using Method 2 because it looks like a single value is being passed rather than an array.

Can you give an example of when Method 2 MUST be used instead of Method 1?

Upvotes: 2

Views: 391

Answers (1)

francescalus
francescalus

Reputation: 32366

Consider the program

  implicit none
  integer :: x(2,2)=0
  call set(x(2,1))
  print*, x

contains

  subroutine set(y)
    integer y(2)
    y = [1,2]
  end subroutine set

end program

The dummy argument y in this subroutine call is argument associated with the elements x(2,1) and x(1,2). There is no array section of x which consists of exactly these two elements.

Upvotes: 2

Related Questions