Recursive procedures in subroutines

Dear friends, I have to perform a recursive procedure within a subroutine, something, for example, like this:

module xxxx

subroutine rrrr ! (for simplicity I don’t state the intent of the variables)

A = A + b*50

end subroutine rrrr
end module xxxx

program yyyy
use xxxx

do i = 1,100
call rrrr()
end do

end program yyyy

My doubt is that I do not know where to put the initial value of A = 0. For example, if I place it within the module before the subroutine, every time the subroutine is called, will A be set = 0? In this case, it won’t work. Therefore, could you please help me understand where A should be placed in such a code?

Many thanks for your kind help.

I don’t see recursion in your example.
A rather useless recursive program is given below

program xxxx

  implicit none

  print*, rrrr(100.)


recursive function rrrr(u) result(A)
  real, intent(in) :: u
  real :: A

  print*, 'u', u
  if (u < .5) then
    A = u
    A = rrrr(u/2.)

end function rrrr

end program xxxx

1 Like

Placing it within the module and initialising it to 0 would work.
Placing it inside the recursive procedure and initialing it to 0 would work.

Initialization can only be done

a) as part of a declaration statement, e.g.
REAL:: A=0.

b)separately, in a DATA statement.e.g.
DATA A/0./

c) for a derived type, as default initialization, e.g.
TYPE widget
REAL ::A=0.

TYPE(widget) ::x

an assignment statement (A=0.) does NOT count as initialization.

1 Like

unfortunately not Fortran, but the concepts are similar:

Currently, the course is free of charge.

@themos, @MarDie, many thanks for your help.