# <span class='text-muted'>031.</span> Procedures can be recursive

<span style='font-size: small;' class='text-muted'>topic: {ref}`procedures`</span>

Fortran procedures declared `recursive` can call themselves.

```{literalinclude} ../../src/recursive.f90
:language: fortran
:caption: recursive.f90 | <a href="https://github.com/zmoon/FortranTipBrowser/blob/main/src/recursive.f90" target="_blank" title="See this source on GitHub"><i class="fab fa-github"></i></a> | <a href="https://github.com/Beliavsky/FortranTip/blob/main/recursive.f90" target="_blank" title="See the original source on Beliavsky/FortranTip GitHub"><i class="fab fa-github"></i><sub>0</sub></a> | <a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:fortran,selection:(endColumn:1,endLineNumber:30,positionColumn:1,positionLineNumber:30,selectionStartColumn:1,selectionStartLineNumber:30,startColumn:1,startLineNumber:30),source:'!!+Demonstrate+an+%60elemental+recursive%60+function%0Amodule+m%0A+++implicit+none%0A%0Acontains%0A%0A+++elemental+recursive+function+factorial(n)+result(nfac)%0A++++++integer,+intent(in)+::+n%0A++++++integer+::+nfac%0A%0A++++++if+(n+%3C+0)+then%0A+++++++++nfac+%3D+-1%0A+++++++++return%0A++++++else+if+(n+%3C+2)+then%0A+++++++++nfac+%3D+1%0A+++++++++return%0A++++++else%0A+++++++++nfac+%3D+n*factorial(n+-+1)%0A++++++end+if%0A+++end+function+factorial%0A%0Aend+module+m%0A%0Aprogram+xfactorial%0A+++use+m,+only:+factorial%0A+++implicit+none%0A%0A+++print+*,+factorial(%5B-1,+0,+1,+4%5D)++!!+-1+1+1+24%0A%0Aend+program+xfactorial%0A'),l:'5',n:'0',o:'Fortran+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:gfortran112,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:fortran,libs:!(),options:'',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1,tree:'1'),l:'5',n:'0',o:'x86-64+gfortran+11.2+(Fortran,+Editor+%231,+Compiler+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',m:62.300683371298405,n:'0',o:'',t:'0'),(g:!((h:output,i:(compiler:1,editor:1,fontScale:14,fontUsePx:'0',tree:'1',wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+gfortran+11.2+(Compiler+%231)',t:'0')),header:(),l:'4',m:37.699316628701595,n:'0',o:'',s:0,t:'0')),l:'3',n:'0',o:'',t:'0')),version:4" target="_blank" title="Open in Godbolt Compiler Explorer"><img src="https://raw.githubusercontent.com/compiler-explorer/compiler-explorer/main/views/resources/site-logo.svg" alt="Godbolt Compiler Explorer logo" width="55.11" height="16.7" class="align-text-bottom" /></a> | <a href="https://play.fortran-lang.org/?code=%21%20Demonstrate%20an%20%60elemental%20recursive%60%20function%0Amodule%20m%0A%20%20%20implicit%20none%0A%0Acontains%0A%0A%20%20%20elemental%20recursive%20function%20factorial%28n%29%20result%28nfac%29%0A%20%20%20%20%20%20integer%2C%20intent%28in%29%20%3A%3A%20n%0A%20%20%20%20%20%20integer%20%3A%3A%20nfac%0A%0A%20%20%20%20%20%20if%20%28n%20%3C%200%29%20then%0A%20%20%20%20%20%20%20%20%20nfac%20%3D%20-1%0A%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20else%20if%20%28n%20%3C%202%29%20then%0A%20%20%20%20%20%20%20%20%20nfac%20%3D%201%0A%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20nfac%20%3D%20n%2Afactorial%28n%20-%201%29%0A%20%20%20%20%20%20end%20if%0A%20%20%20end%20function%20factorial%0A%0Aend%20module%20m%0A%0Aprogram%20xfactorial%0A%20%20%20use%20m%2C%20only%3A%20factorial%0A%20%20%20implicit%20none%0A%0A%20%20%20print%20%2A%2C%20factorial%28%5B-1%2C%200%2C%201%2C%204%5D%29%20%20%21%20-1%201%201%2024%0A%0Aend%20program%20xfactorial%0A" target="_blank" title="Open in Fortran Playground"><img src="https://raw.githubusercontent.com/fortran-lang/playground/main/frontend/src/fortran-logo.png" alt="Fortran logo" height="15.5" class="align-text-bottom" /></a>
```

```{code-block} text
:caption: Output[^gfortran-version]

          -1           1           1          24

```

[^gfortran-version]: Compiled using `GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0` with no flags

````{warning}

In GFortran v9 and v10 (and lower versions, presumably),
this isn't allowed, and we get:
```text
Error: ELEMENTAL attribute conflicts with RECURSIVE attribute
```
````

---

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Created with <a href="https://twitter.com/carbon_app?ref_src=twsrc%5Etfw">@carbon_app</a> <a href="https://t.co/czICWzekYU">pic.twitter.com/czICWzekYU</a></p>&mdash; FortranTip (@fortrantip) <a href="https://twitter.com/fortrantip/status/1472702427740745728?ref_src=twsrc%5Etfw">December 19, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>