Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to pas an overloaded subroutine as an argument to another subroutine

0 views
Skip to first unread message

Luksa

unread,
Aug 20, 2007, 8:48:34 AM8/20/07
to
Hi!

I'd like to pass an overloaded procedure as an argumet to the other
procedure, but compiler doesn't let me do it.

How can i do something like the following:

File1:

module some_module_ml

public :: a

interface a
module procedure a_int, a_real
end interface a
contains
subroutine a_int(i)
integer, intent(in) :: i
...
end subroutine a_int
subroutine a_real(x)
real, intent(in) :: x
...
end subroutine a_real
end module some_module_ml

File2:
program some_program
use some_module_ml, only : a

cal some_other_subroutine(a)

contains
subroutine some_other_subroutine(a)
interface
subroutine a(i)
integer, intent(in) :: i
end subroutine a
end interface
...
end subroutine some_other_subroutine

Thanks!

alexzenk

unread,
Aug 20, 2007, 4:35:23 PM8/20/07
to

Hi!
I think you will not be able to send overloaded procedure as
argument.
Better send the pointer on each procedure. For instance so:

MODULE some_module_ml

CONTAINS
SUBROUTINE a_int(i)
INTEGER, INTENT(in) :: i
write(*,*) " a int", i
! ...
END SUBROUTINE a_int
SUBROUTINE a_real(x)
REAL, INTENT(in) :: x
write(*,*) " a REAL", x
!...
END SUBROUTINE a_real
END MODULE some_module_ml

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
PROGRAM some_program
USE some_module_ml
INTEGER PointerToa_int
INTEGER PointerToa_real
PointerToa_int = LOC(a_int)
PointerToa_real = LOC(a_real)
!...............................
CALL some_other_subroutine(PointerToa_int)
!..................................
CALL some_other_subroutine(PointerToa_real)
CONTAINS
SUBROUTINE some_other_subroutine(Pa)
INTEGER Pa
IF(Pa == PointerToa_int) THEN
i = 1
CALL a_int(i)
ELSE IF(Pa == PointerToa_real) THEN
x =2
CALL a_real(x)
END IF
!...
END SUBROUTINE some_other_subroutine
END

Possible also variants with use Cray pointers
http://gcc.gnu.org/onlinedocs/gfortran/Cray-pointers.html#Cray-pointers

0 new messages