Is there a limit to the number of layers in nested function/subroutine?
Yes, the limit is one layer.
A subprogram may have internal procedures, and these are considered different things in the standard. A subprogram is either a main program, a procedure in a module, or an external procedure. Internal procedures follow a contains
statement in one of those things, and internal procedure cannot contain internal procedures, thus the limit is one layer of nesting.