|
From: mabrowning on 11 Apr 2008 17:00 One more edit: I've gotten something to work, but I am unhappy with it. MODULE fun CONTAINS FUNCTION func(t) REAL :: t REAL, DIMENSION(2,2)::func func(1,1)=1*t func(1,2)=2*t func(2,1)=3*t func(2,2)=4*t END FUNCTION func END MODULE fun MODULE sub CONTAINS SUBROUTINE subr(A) INTERFACE FUNCTION A(t) REAL :: t REAL, DIMENSION(2,2)::A END FUNCTION END INTERFACE WRITE(*,*)A(1.0) END SUBROUTINE subr END MODULE sub PROGRAM main use sub use fun WRITE(*,*)func(1.0) CALL subr(func) END PROGRAM main --------------------- Requiring the INTERFACE be in the subroutine is less than optimal. Ideally, the size would be variable and not hard coded...
From: Richard Maine on 11 Apr 2008 17:27 mabrowning <mabrowningrr(a)gmail.com> wrote: .... > SUBROUTINE subr(A) > INTERFACE > FUNCTION A(t) > REAL :: t > REAL, DIMENSION(2,2)::A > END FUNCTION > END INTERFACE > WRITE(*,*)A(1.0) > END SUBROUTINE subr > END MODULE sub .... > Requiring the INTERFACE be in the subroutine is less than optimal. Anything you declare about a dummy argument of a procedure must be declared in the procedure. The argument doesn't exist anywhere else. I can't imagine where else you might think it could be declared. Don't confuse dummy and actual arguments; they are not at all the same thing. Declaring things about an actual argument does *NOT* declare them about the dummy. So you aren't going to get out of declaring the dummy argument. What you can get out of is writing out the whole interface body. You have to wait for f2003, though. With f2003 you can use a PROCEDURE statement to declare that, for example, the interface to the dummy is the same as the interface of some other procedure. This can cut down on the excess verbiage and opportunity for error. But it stil involved declaring the dummy argument correctly. > Ideally, the size would be variable and not hard coded... It can be... See assumed shape. However, in any case, the interface of the dummy has to agree with the interface of the actual. You can't pass an actual argument procedure that uses explicit shape to a dummy procedure that uses assumed shape. That just won't work and for good reasons. Much of the point of explicit interfaces is so that the compiler will know how to generate the calling code correctly. The code for assumed-shape arguments tends to be majorly different from that for explicit-shape arguments. The code for assumed shape has to include a mechanism to pass the shape, and there also tend to be differences relating to contiguity requirements. If you tell the compiler that assumed shape is to be used, but then you pass a procedure that uses explicit shape instead, it just isn't going to work. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
From: fj on 11 Apr 2008 17:30
On 11 avr, 23:00, mabrowning <mabrownin...(a)gmail.com> wrote: > One more edit: > > I've gotten something to work, but I am unhappy with it. > > MODULE fun > CONTAINS > FUNCTION func(t) > REAL :: t > REAL, DIMENSION(2,2)::func > func(1,1)=1*t > func(1,2)=2*t > func(2,1)=3*t > func(2,2)=4*t > END FUNCTION func > END MODULE fun > > MODULE sub > CONTAINS > SUBROUTINE subr(A) > INTERFACE > FUNCTION A(t) > REAL :: t > REAL, DIMENSION(2,2)::A > END FUNCTION > END INTERFACE > WRITE(*,*)A(1.0) > END SUBROUTINE subr > END MODULE sub > > PROGRAM main > use sub > use fun > WRITE(*,*)func(1.0) > CALL subr(func) > END PROGRAM main > > --------------------- > Requiring the INTERFACE be in the subroutine is less than optimal. > Ideally, the size would be variable and not hard coded... I generally prefer subroutines when I want to get variable length output. For instance you could define FUNC as follows : SUBROUTINE FUNC(t,r) REAL :: t REAL, DIMENSION(:,:)::r ... END SUBROUTINE The interface repeats the same definition of arguments and the routine subr receives now two arguments, the subroutine and the output array : SUBROUTINE subr(A,v) REAL,DIMENSION(:,:) :: v INTERFACE SUBROUTINE A(t,v) REAL :: t REAL, DIMENSION(:,:)::v END SUBROUTINE END INTERFACE CALL a(v) write(*,*) v END SUBROUTINE subr |