Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

modules vs. block interfaces

zyserman
Beginner
1,136 Views
Hi all,
I have the following two questions:

1) I have read from different sources (including Steve Lionel's Doctor Fortran blog) that in order to create an
explicit interface for an external procedure, modules are to be preferred to block interfaces. Why is this so?

2) Assume I want to pass a function as an argument of a subroutine. The declaration of EXTERNAL leaves
the function without an explicit interface; and I have seen that it is possible to create it using block interfaces,
my example is the following:
Main program:
program main
use interfaces
implicit none
real::resulto
call promedio(func,0.0,1.0,resulto)
write(*,*) resulto
end program main

The subroutine:
subroutine promedio(f,a,b,resulto)
implicit none
real, intent(in) :: a
real, intent(in) :: b
real, intent(out):: resulto

real::c=2.

interface
real function f(x)
implicit none
real, intent(in) :: x
end function f
end interface


resulto = 0.5*(f(a,c)+f(b))
return

end subroutine promedio

The function:
real function func(x)
implicit none
real, intent(in) :: x
func=x**2
end function func

The interfaces, included in a module (of course, they could have been in the main program):

module interfaces
!
interface
subroutine promedio(f,a,b,result)
implicit none
real, intent(in) :: a
real, intent(in) :: b
real, intent(out) :: result

interface
real function f(x)
implicit none
real, intent(in) :: x
end function f
end interface

end subroutine
end interface
!
interface
real function func(x)
implicit none
real, intent(in) :: x
end function func
end interface

end module interfaces


With this structure, if the function in the subroutine is accidentally called with more variables than
declared, the compiler complains.

My point is the following: Could this have been done without using interface blocks, and using modules?
I was not able to find the way.

Hope someone can help.

Regards,

Fabio Zyserman





0 Kudos
2 Replies
TimP
Honored Contributor III
1,136 Views
You raise an interesting point about checking dummy procedure calls. I suppose checking via the module definition would require an extension of interprocedural analysis, as syntactically there is no association until run time. So you have to supply an interface block with the name of the function as used at the call site.
0 Kudos
Steven_L_Intel1
Employee
1,136 Views
When I wrote that you should use module procedures instead of interface blocks, I meant that the combination of F77-style separate procedures and interface blocks was not a good practice, since you have to specify the interface twice. In your example, function f could be a module procedure and then you would not have to declare its interface separately in promedio.

You do still need an interface block in the declaration of the dummy argument - in F77 this would be EXTERNAL. Fortran 2003 allows use of abstract interface and PROCEDURE declarations here - coming soon to Intel Fortran.

A module containing interface blocks that declare Fortran procedures is bad form. Make those Fortran procedures module procedures instead.
0 Kudos
Reply