PDA

View Full Version : Pointers for functions???--Usage of QDAG



sreekanth_aka
01-15-2007, 12:23 AM
Hello

Lets consider a subroutine with a function (F) as one of its arguments

subroutine QDAG(F,A,B...)
.
.
real external :: F
.
.
.
r=F(u)......(1)
..
end subroutine

In the main program if i call this subroutine with

Call QDAG(G,A,B..) ! Please note the different name for the function
! argument

and if the function is written with this name G, i suspect that there will be a problem. when the compiler reaches the point(1) (pls see above), it will search for an external function named F not G. How can this be resolved??. In MATLAB, function handles are used but in fortran i am have no clue. Are there any pointers for function arguments??..

Thank you

SA

Richard Hanson
01-15-2007, 05:36 AM
You should get a book on F90 and practice exercises in passing function names. There are two attached codes that are slight variations on what was mentioned in replying to your previous post. You seem to want to use different names than F(X), say G(X).

The first one uses an external routine named G(X). The second uses a routine G(X) that is packaged with the main program unit. It integrates the function d/(x+c). The value d is visible because it is found in the scoping of
G(x). The value of c comes from the module MY_QDAG_INT. Note that the first example requires an EXTERNAL statement for G but the second one does not, because G is not external but part of the CONTAINS scoping. Both codes were executed so you should be able to cut and paste to verify.

First code:

Module My_QDAG_INT
USE QDAG_INT ! VNI provides this module for you
IMPLICIT NONE
include 'link_f90_static.h' ! Not needed except for Intel compilers
REAL c ! This is the value to be passed to F(X)
End Module

Program MY_USE_of_QDAG
USE My_QDAG_INT
REAL RESULT
EXTERNAL G
C = 1. ! This sets the value to use in F(X)
call qdag(g, 0., 1., RESULT)

end Program

FUNCTION g(X)
USE MY_QDAG_INT
REAL g, x
g = x+c ! The current value of c is made visible
! via the module MY_QDAG_INT
write(*,*) C
End Function
Second code:

Module My_QDAG_INT
USE QDAG_INT ! VNI provides this module for you
IMPLICIT NONE
include 'link_f90_static.h' ! Not needed except for Intel compilers
REAL c ! This is the value to be passed to F(X)
End Module

Program MY_USE_of_QDAG
USE My_QDAG_INT
REAL RESULT, d
C = 1. ! This sets the value to use in G(X)
d = 3. ! Another value to be used in G(X)
call qdag(g, 0., 1., RESULT)
CONTAINS
FUNCTION g(X)
USE MY_QDAG_INT
REAL g, x
g = d/(x+c) ! The current value of c is made visible
! via the module MY_QDAG_INT
write(*,*) C, D
End Function

end Program

Cheers and good luck. Try some exercises to get familiar with the concepts.