F95: array-returning function as argument to another function 
Author Message
 F95: array-returning function as argument to another function

Hello,
I tried to use an array-returning function as argument to another
function - but my approach (see below) doesn't work. If I use scalar
integers instead of arrays (the comments) everything is fine. So what is
my mistake?

Carsten

program arrayTest
implicit none
integer :: f1, a
interface
    function f2()
    implicit none
    integer, dimension(10)  :: f2
!    integer :: f2
    end function f2
end interface
external f1

    a = f1(f2)
    print *, a

end program arrayTest

integer function f1( f )
implicit none
integer, dimension(10)  :: f, a
!integer :: f, a

    a = f()
    print *, a
    f1 = 20

end function

function f2()
implicit none
integer, dimension(10)  :: f2
!integer :: f2

    f2 = 10

end function f2



Mon, 17 May 2004 18:34:34 GMT  
 F95: array-returning function as argument to another function


Wed, 18 Jun 1902 08:00:00 GMT  
 F95: array-returning function as argument to another function

Quote:
> Hello,
> I tried to use an array-returning function as argument to another
> function - but my approach (see below) doesn't work. If I use scalar
> integers instead of arrays (the comments) everything is fine. So what is
> my mistake?

An array-valued function needs an explicit interface wherever it is
referenced.  You added an interface in the main program, but where it is
really needed is in function f1 which calls the array function.  As you have
it written, in f1, f is a local variable (and presumed to be an array).  In
F77 you would normally have said EXTERNAL F, but that doesn't provide the
required explicit interface.  So declare argument f with an INTERFACE as
well and you should be all set.

--

Steve Lionel
Compaq fortran Engineering
Intel Corporation
Nashua, NH

Compaq Fortran - http://www.compaq.com/fortran
Intel Fortran - http://developer.intel.com/software/products/compilers/f50/



Mon, 17 May 2004 18:53:34 GMT  
 F95: array-returning function as argument to another function
Why use messy errorprone interfaces to external routines at all, just use
internal routines..
You can still get the desirable effect of dealing with a smaller exec source
(if thats a concern) by segregating internals in an INCLUDE (see below)..
Plus if there is a interface problem, the compiler can see it at compile
time vs. a run-time error.

program test
write (*,*) f1()      ! outputs 10 values = 20
stop
contains
include "test.inc"
end program

test.inc
--------
function f1()  result (out)
integer :: out(10)
out = f2() +10
end function

function f2()  result (out)
integer :: out(10)
out = 10
end function
end program



Mon, 17 May 2004 21:16:48 GMT  
 F95: array-returning function as argument to another function


Wed, 18 Jun 1902 08:00:00 GMT  
 F95: array-returning function as argument to another function
Carsten:  In case it my help,
posted in this space about a year or so ago, by Mirek Gruszkiewicz
and others were three slightly different ways to construct a function
that returns arrays. These three codes are:
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/farray1.f90
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/farray2.f90
and
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/farray3.f90

Note that all three codes compile and run correctly via Windows compilers:
CVF 6.6, LF95 5.6g, and Salford FTN95 2.53m, and
AIX compiler XLF90 7.01.

P.S. Apologies to "othes" who supplied the above codes for
farray2 and farray3. If you would please identify yourself I'll
add your name to the comments of same.

Skip Knoble, Penn State



-|Hello,
-|I tried to use an array-returning function as argument to another
-|function - but my approach (see below) doesn't work. If I use scalar
-|integers instead of arrays (the comments) everything is fine. So what is
-|my mistake?
-|
-|Carsten
-|
-|
-|
-|program arrayTest
-|implicit none
-|integer :: f1, a
-|interface
-|    function f2()
-|    implicit none
-|    integer, dimension(10)  :: f2
-|!    integer :: f2
-|    end function f2
-|end interface
-|external f1
-|
-|    a = f1(f2)
-|    print *, a
-|
-|end program arrayTest
-|
-|
-|
-|integer function f1( f )
-|implicit none
-|integer, dimension(10)  :: f, a
-|!integer :: f, a
-|
-|    a = f()
-|    print *, a
-|    f1 = 20
-|    
-|end function
-|
-|
-|
-|function f2()
-|implicit none
-|integer, dimension(10)  :: f2
-|!integer :: f2
-|
-|    f2 = 10
-|    
-|end function f2

   Herman D. (Skip) Knoble, Research Associate

   Web: http://www.personal.psu.edu/hdk
   Center for Academic Computing
   Penn State University
     214C Computer Building
     University Park, PA 16802-2101
   Phone:+1 814 865-0818   Fax:+1 814 863-7049



Mon, 17 May 2004 21:50:19 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Passing array valued functions as argument to function.

2. Help on Function Return Arguments

3. F77-C: Passing Fortran FUNCTION as an argument for a C Function

4. Intrinsoc function as argument to intrinsic function question

5. tcl+C function with pointer function argument?

6. function that returns function

7. how to write function that returns function

8. how to write function that returns function

9. about functions that return functions.

10. Passing array as argument to function

11. multi-dimension arrays as task/function arguments

12. array and scalar function arguments

 

 
Powered by phpBB® Forum Software