Silverfrost Forums

Welcome to our forums

Array argument for a statement function

20 Sep 2023 11:11 #30581

I am trying very hard to break the habit of using nested do loops for everything, i.e. use array operations where possible.

In the following code, I think I am breaking the rules of Fortran, but FTN95 does not report any ERROR or WARNING.

What I see here is FTN95 accepting an array argument for a one line statement function, which I think is not allowed?

Is my understanding correct (a bug) or am I missing something in my understanding?

program p2
implicit none
integer, parameter :: n = 4
real*8 :: x(n) = 0, y(n) = 0, z(n,n) = 0
integer i,j
real*8  f, a, b
!Statement function to define a function f of two scalars x and y.
   f(a,b) = a + b
 
   x = [1,2,3,4]
   y = [1,2,3,4]

   do i = 1, n
     z(:,i) = f(x(:), y(i))    ! Should FTN95 not generate a fatal ERROR here?
   end do

   print*, 'Using array operation to eliminate one do loop and a statement function'
   do i = 1, n
     print*, (z(i,j),j = 1,n)
   end do
   print*

!  This is allowed because f2 is elemental
   do i = 1, n
     z(:,i) = f2(x(:), y(i))
   end do

   print*, 'Using array operation to eliminate one do loop using elemental function'
   do i = 1, n
     print*, (z(i,j),j = 1,n)
   end do

   contains

   real*8 elemental function f2(c,d)
   real*8, intent(in) :: c, d
     f2 = c+d
   end function f2
   
end program p2

i.e. Is the assigment:

z(:,i) = f(x(:), y(i)) 

valid fortran in this context?

Please login to reply.