# Subroutines contained in a function

**URL:** <https://fortran-lang.discourse.group/t/subroutines-contained-in-a-function/3416>\
**Category:** Help\
**Created:** [May 9, 2022, 6:39am UTC](https://fortran-lang.discourse.group/t/subroutines-contained-in-a-function/3416 "2022-05-09T06:39:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fortran4r](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fortran4r](https://fortran-lang.discourse.group/u/fortran4r)\
**Post date:** [May 9, 2022, 6:39am UTC](https://fortran-lang.discourse.group/t/subroutines-contained-in-a-function/3416/1 "2022-05-09T06:39:12Z")

</div>

Is it OK to have subroutines contained in a function? I have never seen this kind of style.

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [May 9, 2022, 9:39am UTC](https://fortran-lang.discourse.group/t/subroutines-contained-in-a-function/3416/2 "2022-05-09T09:39:01Z")

</div>

There are several reasons why that kind of subprogram is troublesome and inelegant, but it is possible. Here is a contrived example.

```auto
program pfib 
   implicit none 
   integer fib 
   Print *,fib (25)
end program

recursive integer function fib (n) ! "recursive" attribute not employed, in reality
   integer, intent (in)::n 
   if (n == 0) then 
      fib = 0
   elseif (n == 1) then 
      fib = 1
   else
      call fibsub (n, fib)
   endif
    
   contains
      subroutine fibsub (n, ifib)
         integer, intent (in) :: n
         integer, intent (out) :: ifib
         
         integer, save :: sfib(0:30)
         logical :: begin = .true.
         integer i
  
         if(begin)then
            sfib(0:1) = [0,1]
            do i = 2, 30
               sfib(i) = sfib(i-2) + sfib(i-1)
            end do
            begin = .false.
         endif
         ifib = sfib(n)
         return 
     
      end subroutine fibsub 
      
   end function fib 

```

Try to modify this program to make the function **pure** , and you may note several of the demerits that I alluded to.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [May 9, 2022, 6:09pm UTC](https://fortran-lang.discourse.group/t/subroutines-contained-in-a-function/3416/3 "2022-05-09T18:09:34Z")

</div>

Correct me if I am wrong but the only reason the subroutine `fibsub` (and as a consequence, also the function `fib`) is not pure is the presence of local variables with `save` attribute (`sfib` (explicit) and `begin` (implicit, as initialized)). And this does not have anything with it being a subroutine. If I understand the OP, the question was whether it is OK for a `function` to contain a `subroutine`.

---

<div class="post-metadata">

**Author:** ![conradoat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/conradoat/32/1537_2.png) [@conradoat](https://fortran-lang.discourse.group/u/conradoat)\
**Post date:** [May 9, 2022, 6:34pm UTC](https://fortran-lang.discourse.group/t/subroutines-contained-in-a-function/3416/4 "2022-05-09T18:34:31Z")

</div>

Here it is affirmed that you can nest subroutines in order to increase the chances that this part of the code gets inlined.

> <https://stackoverflow.com/questions/42386065/inlining-functions-in-fortran>

I use this programming style from time to time, when a function is only called in a particular point of my code (for trivial tasks such as counting the number of lines in an input file or the number of lines in the header of a data file), and I have never encountered any problem at all with that. Maybe, problems arise with more “esoteric” things to be done.

If this is not an advised style, I will try to avoid it the future.
