Silverfrost Forums

Welcome to our forums

Pure functions as procedure arguments

30 Oct 2010 2:04 #7108

I found a bug in the compiler (version 5.5). When passing a pure function as an argument to another pure function, the compiler gives an error that the argument is not declared as INTENT (IN). But this is not required, and somewhat meaningless for function arguments I believe. (I'm compiling in Checkmate mode but that shouldn't matter).

e.g. I write some code which finds the maximum of a function passed in as an argument on some interval (A, B). I get the error with this code (I provide only a dummy implementation here).

MODULE CALCULUS

CONTAINS

   PURE FUNCTION MAXIMISE(A, B, FUNC)
      REAL, INTENT (IN) :: A, B
      INTERFACE
         PURE FUNCTION FUNC(X)
            REAL, INTENT (IN) :: X
            REAL :: FUNC                  !<< Compiler complains here
         END FUNCTION FUNC
      END INTERFACE

      ! Here I just return the value mid-way between A and B
      MAXIMISE = FUNC((A+B)*0.5)

   END FUNCTION MAXIMISE

END MODULE CALCULUS

If I remove the PURE specifiers it compiles ok. I think this should be easy to fix for the next release. Any thoughts Paul? 😄

30 Oct 2010 3:41 #7109

I think you are right about it probably being easy to fix but the next release is currently being built.

Try using /ignore for the given error number to see if this will provide a work-around for now.

2 Nov 2010 6:27 #7111

Thanks for the quick response and confirmation Paul. I didn't know the new release was so close.

Well you can fix this in the release after that 😄.

I will play with ignoring the error number, but my current work-around is to take out the PURE keywords. I'm not sure the compiler takes much notice anyway in terms of generating efficient code, but only does extra compiler checks.

10 Jan 2011 11:34 #7427

I'm using version 6 of FTN95 now.

I have put the pure keywords back in as i feel it helps me to document what is pure and what is not.

I have added /ignore 826 as a temporary fix. Trouble is, I have missed some genuine errors as this fix is a little aggressive.

Incidently other compilers I have tried (intel, g95, gfortran, absoft) don't have this bug. 😉

10 Jan 2011 3:12 #7434

I am looking at this right now and the short term fix is to add the INTENT(IN) attribute for the procedure argument ...

MODULE CALCULUS 

CONTAINS 

   PURE FUNCTION MAXIMISE(A, B, FUNC) 
      REAL, INTENT (IN) :: A, B 
      INTERFACE 
         PURE FUNCTION FUNC(X) 
            REAL, INTENT (IN) :: X 
            REAL, INTENT (IN) :: FUNC        !<< Compiler does not complain here 
         END FUNCTION FUNC 
      END INTERFACE 

      ! Here I just return the value mid-way between A and B 
      MAXIMISE = FUNC((A+B)*0.5) 

   END FUNCTION MAXIMISE 

END MODULE CALCULUS 
10 Jan 2011 3:55 #7436

This bug has now been fixed for the next release.

10 Jan 2011 11:48 #7447

Thanks Paul, it's good to know you have fixed it.

11 Jan 2011 1:52 #7453

In the code example above, should MAXIMISE be declared as REAL ?

11 Jan 2011 7:34 #7456

Quoted from JohnCampbell In the code example above, should MAXIMISE be declared as REAL ?

Yes, you are correct. As is stands MAXIMISE is an integer function.

Please login to reply.