forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Array argument for a statement function

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Kenneth_Smith



Joined: 18 May 2012
Posts: 697
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Wed Sep 20, 2023 12:11 pm    Post subject: Array argument for a statement function Reply with quote

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?

Code:
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:
Code:
z(:,i) = f(x(:), y(i))

valid fortran in this context?
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group