billgray1234
Programmer
- Mar 14, 2011
- 39
i'm using fortran 90/95.
i'm using recursive subroutines.
i want to use an optional argument within one of these subroutines. i'm trying to determine whether i can.
basically, i want to know if the following situation is valid :-
1) a recursive subroutine has the optional argument declared as "optional" in it's (dummy) argument list
2) the FIRST call to the recursive subroutine is made from a "calling procedure". this "calling procedure" is either a) the main program, or b) another subroutine -- but is NOT the recursive subroutine
3) the optional argument is NOT 'present' in the "calling procedure". therefore it is also NOT 'present' in the FIRST USE (i.e. FIRST CALL) of the recursive subroutine
4) during the FIRST USE of the recursive subroutine, the optional argument is "created"
5) during SUBSEQUENT calls of the recursive subroutine (i.e. each time the recursive subroutine calls itself), the optional argument is included in the recursive subroutine's (actual) argument list. hence, from the SECOND CALL onwards, the optional argument SHOULD be 'present' in the recursive subroutine's (dummy) argument list.
an example of this is given below. note that, in my actual code, each of the subroutines shown below is stored in it's own individual module (for various reasons). for simplicity, i've omitted this detail below.
---------------------------------------
*** CALLING CODE ***
SUBROUTINE F ( X1 , X2 )
INTEGER ,INTENT (INOUT) :: X1
INTEGER ,INTENT (INOUT) :: X2
! initialise X1
X1 = 1
! first call to recursive subroutine
CALL G ( X1 , X2 )
END SUBROUTINE F
---------------------------------------
*** RECURSIVE SUBROUTINE ***
RECURSIVE SUBROUTINE G ( X1 , X2 , A )
INTEGER ,INTENT (INOUT) :: X1
INTEGER ,INTENT (INOUT) :: X2
REAL ,INTENT (IN) ,OPTIONAL ,DIMENSION ) :: A
INTEGER :: IA
INTEGER :: IB
REAL, ALLOCATABLE, DIMENSION ) :: B
! construct temporary vector B (if necessary)
IF ( .NOT. ( PRESENT (A) ) ) THEN
! determine the size that is needed for A
...IA = 3, say
! allocate B the amount of space that is needed for A
...IB = IA
...ALLOCATE ( B (IB) )
! hence, calculate values for B
...B (1) = some calculation
...B (2) = some calculation
...B (3) = some calculation
END IF
! either call this subroutine again (with B substituted for A), or perform some "final" calculation
IF ( X1 < 5 ) THEN
...X1 = X1 + 1
...CALL G ( X1 , X2 , B )
ELSE
...perform some "final" calculation, using X2 and A
...RETURN
END IF
END SUBROUTINE G
i'm using recursive subroutines.
i want to use an optional argument within one of these subroutines. i'm trying to determine whether i can.
basically, i want to know if the following situation is valid :-
1) a recursive subroutine has the optional argument declared as "optional" in it's (dummy) argument list
2) the FIRST call to the recursive subroutine is made from a "calling procedure". this "calling procedure" is either a) the main program, or b) another subroutine -- but is NOT the recursive subroutine
3) the optional argument is NOT 'present' in the "calling procedure". therefore it is also NOT 'present' in the FIRST USE (i.e. FIRST CALL) of the recursive subroutine
4) during the FIRST USE of the recursive subroutine, the optional argument is "created"
5) during SUBSEQUENT calls of the recursive subroutine (i.e. each time the recursive subroutine calls itself), the optional argument is included in the recursive subroutine's (actual) argument list. hence, from the SECOND CALL onwards, the optional argument SHOULD be 'present' in the recursive subroutine's (dummy) argument list.
an example of this is given below. note that, in my actual code, each of the subroutines shown below is stored in it's own individual module (for various reasons). for simplicity, i've omitted this detail below.
---------------------------------------
*** CALLING CODE ***
SUBROUTINE F ( X1 , X2 )
INTEGER ,INTENT (INOUT) :: X1
INTEGER ,INTENT (INOUT) :: X2
! initialise X1
X1 = 1
! first call to recursive subroutine
CALL G ( X1 , X2 )
END SUBROUTINE F
---------------------------------------
*** RECURSIVE SUBROUTINE ***
RECURSIVE SUBROUTINE G ( X1 , X2 , A )
INTEGER ,INTENT (INOUT) :: X1
INTEGER ,INTENT (INOUT) :: X2
REAL ,INTENT (IN) ,OPTIONAL ,DIMENSION ) :: A
INTEGER :: IA
INTEGER :: IB
REAL, ALLOCATABLE, DIMENSION ) :: B
! construct temporary vector B (if necessary)
IF ( .NOT. ( PRESENT (A) ) ) THEN
! determine the size that is needed for A
...IA = 3, say
! allocate B the amount of space that is needed for A
...IB = IA
...ALLOCATE ( B (IB) )
! hence, calculate values for B
...B (1) = some calculation
...B (2) = some calculation
...B (3) = some calculation
END IF
! either call this subroutine again (with B substituted for A), or perform some "final" calculation
IF ( X1 < 5 ) THEN
...X1 = X1 + 1
...CALL G ( X1 , X2 , B )
ELSE
...perform some "final" calculation, using X2 and A
...RETURN
END IF
END SUBROUTINE G