Is it possible in Fortran to define a subroutine within another subroutine? When I try something like
SUBROUTINE test1(...)
! do some stuff
CALL test2(...)
SUBROUTINE test2(...)
! do some stuff
END SUBROUTINE test2
END SUBROUTINE test1.
my compiler (Silverfrost) gives me a recursion error.
As HighPerformanceMark comments, it is possible to define an internal procedure in the contains section of another procedure
The internal procedure
test2gets access to all entities defined in the host proceduretest1by host association. It also gets access to all entities thattest1has access to. The rules are similar to the rules of internal procedures in the main program.An internal procedure cannot host another internal procedure.
Procedure pointers to internal procedures are only valid during the execution of the host procedure and are only allowed in Fortran 2008 and later. This is an advanced feature.
This host association can sometimes be annoying, you must be vigilant to avoid bugs like:
There will be features in Fortran 2015 to change the host association behaviour.