functions returning (pointer to) subroutine? 
Author Message
 functions returning (pointer to) subroutine?

Hi all.

Quite a lot of people asked how to return arrays or character strings from
function, but I could not find a reference whether anyone tried to return a
subroutine name from a function?

I am aware that the approach is very C-ish, nevertheless: it is possible to
pass a subroutine name as an actual argument to another subroutine, which
in turn, may call the subroutine whose name was passed. Now consider a
function returning the name of a subroutine which in turn may be used as an
actual argument for another routine.

In the code below, the main program calls SUBROUTINE FOR_EACH which applies
function FUNC to each of the elements in ARRAY (I know about ELEMENTAL, the
array is for demonstration purpose only). If FUNC needs any additional
information, it can not be passed to a predefined FOR_EACH interface,
instead, a functor-module could be used: mod_functor defines a constructor
function that takes the additional information, PREFIX in this case, and
stores it in a place where FUNCTOR may access it afterwards. When the
constructor is done, it shall return the name of the functor subroutine,
i.e. FUNCTOR, which then is used as an actual argument to FOR_EACH.

Is it possible to define FUNCTOR_CONSTRUCTOR in a way, that this code
actually works?

Any hints or pointers would be highly appreciated!

Regards
        Daniel

MODULE mod_for_each
CONTAINS
  SUBROUTINE for_each(array, func)
    INTEGER, DIMENSION(:), INTENT(in) :: array
    INTERFACE
      SUBROUTINE func(x)
        INTEGER, INTENT(in) :: x
      END SUBROUTINE
    END INTERFACE

    INTEGER :: i

    DO i = 1, size(array)
      CALL func(array(i))
    END DO
  END SUBROUTINE
END MODULE

MODULE mod_functor
CHARACTER(len=32) :: prefix
CONTAINS
  FUNCTION functor_constructor(s) RESULT (sub)
    CHARACTER(len=*), INTENT(in) :: s

    !
    ! How to define this return value??
    !
!    INTERFACE
!      SUBROUTINE sub(x)
!        INTEGER, INTENT(in) :: x
!      END SUBROUTINE
!    END INTERFACE
!    sub = functor

    prefix = s
  END FUNCTION

  SUBROUTINE functor(x)
    INTEGER, INTENT(in) :: x
    WRITE(*,*) prefix, x
  END SUBROUTINE
END MODULE

PROGRAM main
  USE mod_for_each
  USE mod_functor

  CALL for_each( (/ 1, 2, 3, 4 /), functor_constructor("line") )
END PROGRAM



Tue, 19 May 2009 08:15:05 GMT  
 functions returning (pointer to) subroutine?

Quote:

> Quite a lot of people asked how to return arrays or character strings from
> function, but I could not find a reference whether anyone tried to return a
> subroutine name from a function?

You are confusing a name with the thing named. You don't actually mean
returning a subroutine name. That you can do, but it isn't what you
want. A name would just be a character string. No, you can't turn a
character string into something callable (and no, you can't do that in C
either). Anyway, that aside...

What you want is a pointer to a procedure. You can do that in f2003, but
not in f95 or earlier. There are various compiler-specific hacks you can
do in earlier versions, but nothing standard or portable.

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain



Tue, 19 May 2009 09:34:55 GMT  
 functions returning (pointer to) subroutine?

Quote:


>> Quite a lot of people asked how to return arrays or character strings
>> from function, but I could not find a reference whether anyone tried to
>> return a subroutine name from a function?

> What you want is a pointer to a procedure. You can do that in f2003, but
> not in f95 or earlier. There are various compiler-specific hacks you can
> do in earlier versions, but nothing standard or portable.

Thanks.


Tue, 19 May 2009 17:27:37 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Returning Pointer To Pointer From Function

2. Pointers to subroutines & functions

3. pointers to functions/subroutines in (SGI) fortran?

4. IVF odd behaviour: subroutine vs function returning floating point number

5. Call Library Function returning a pointer to doubles

6. returning base class pointer from function

7. F90 problem compiling function returning pointer array

8. NT & C function returning pointer

9. HELP F90, External function returning pointer

10. Calling a dll c function returning a pointer

11. passing functions or subroutines in a subroutine

12. Passing functions CONTAINed in a subroutine to other functions

 

 
Powered by phpBB® Forum Software