Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28434 Discussions

Functions are not intrinsically recursive

rudi-gaelzer
New Contributor I
502 Views

According to the standard of F2018, a procedure, except a character(len= *) procedure, is intrinsically recursive, unless qualified with NON_RECURSIVE.

It is said that the intel fortran compiler fully supports F2018, but the well-known example below will not compile unless the attribute RECURSIVE is explicitly written.

program tes_recursive_routine
implicit none
integer :: n

do
   write(*, '(/, a)', advance= 'no')'n= ' ; read(*,*)n
   write(*, '(g0, a, g0)')n, '!= ', fat(n)
end do

CONTAINS
   function fat(n) result (nfat)
   integer :: nfat
   integer, intent(in) :: n
   if(n == 0)then
      nfat= 1
   else
      nfat= n*fat(n - 1)
   end if
   return
   end function fat
end program tes_recursive_routine

 Compilation with ifort returns:

>ifort tes_recursive_routine.f90 
tes_recursive_routine.f90(17): error #6437: A subroutine or function is calling itself recursively.   [FAT]
      nfat= n*fat(n - 1)
--------------^
compilation aborted for tes_recursive_routine.f90 (code 1)
0 Kudos
1 Solution
andrew_4619
Honored Contributor II
446 Views

Intel is quite (rightly so IMO) conservative of standards changes that change the behaviour of old (existing) code as in some case that can break working  code. There are a number of later standards options that as Arjen noted get lumped into the standard-semantics option. 

View solution in original post

0 Kudos
3 Replies
Arjen_Markus
Honored Contributor I
471 Views

It is available via a compile option: -assume:recursion or -standard-semantics (which turns on a number of such options)

0 Kudos
andrew_4619
Honored Contributor II
447 Views

Intel is quite (rightly so IMO) conservative of standards changes that change the behaviour of old (existing) code as in some case that can break working  code. There are a number of later standards options that as Arjen noted get lumped into the standard-semantics option. 

0 Kudos
JNichols
New Contributor I
421 Views

LISP is naturally recursive, so one writes in one fashion, adding recursion to Fortran late does cause potential problems for older code, this is not a natural change and so the change over needs to be sensitive to this fact.  One also has the problem that with old Fortran code, the developers are retired or deceased and so fixing this code for straight recursion is a significant time problem.  

Why recursion was not added before was always an interesting question,  but all languages have problems,  see Turing famous imitation paper for a great discussion on these types of challenging problems.  

Think of US and metric, if you take an American to China and then at 2 am in the morning they need to provide a medicine to a child and this  creates a huge problem as they try and sort out the conversions.  Been there and you need someone who understands both systems or you can hurt the child.  CHILDREN DIE IN THE US FROM THIS MEDICAL MISTAKE EVERY DAY.  

So change is slow.  

 

0 Kudos
Reply