Remix.run Logo
sampo 5 days ago

> Suppose you pass a parameter, N, and you also would like to pass a tensor, and you would like to specify the tensor's shape (N, N).

You can do that, and it might be cleaner and less lines of code that way.

But you don't necessarily need to pass the array dimensions as a parameter, as you can call `size` or `shape` to query it inside your function.

    program main
      implicit none
    
      real :: a(2, 2) = reshape([1., 2., 3., 4.], [2, 2])
    
      call print_array(a)
    
    contains
    
      subroutine print_array(a)
        real, intent(in) :: a(:, :)
        integer :: n, m, i, j
    
        n = size(a, 1) ; m = size(a, 2)
        write(*, '("array dimensions:", 2i3)') [n, m]
        do i = 1, n
          do j = 1, m
            write(*, '(f6.1, 1x)', advance='no') a(i, j)
          end do
          print *
        end do
      end subroutine
    
    end program
thatjoeoverthr 4 days ago | parent [-]

True! I just wanted to highlight it. (I had to do this at an interface because the ABI doesn’t pass the shape.)