Dear friends, I have to perform a recursive procedure within a subroutine, something, for example, like this:
subroutine rrrr ! (for simplicity I don’t state the intent of the variables)
A = A + b*50
end subroutine rrrr
end module xxxx
do i = 1,100
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
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
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.
b)separately, in a DATA statement.e.g.
c) for a derived type, as default initialization, e.g.
an assignment statement (A=0.) does NOT count as initialization.
unfortunately not Fortran, but the concepts are similar:
Currently, the course is free of charge.
@themos, @MarDie, many thanks for your help.