# <span class='text-muted'>028.</span> Define and call a subroutine

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

```{literalinclude} ../../src/subroutine.f90
:language: fortran
:caption: subroutine.f90 | <a href="https://github.com/zmoon/FortranTipBrowser/blob/main/src/subroutine.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/subroutine.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:36,positionColumn:1,positionLineNumber:36,selectionStartColumn:1,selectionStartLineNumber:36,startColumn:1,startLineNumber:36),source:'!!+Define+and+call+a+subroutine%0Aprogram+optional_arg%0A+++implicit+none%0A+++real+::+x(1000000),+xmean,+xsd%0A%0A+++call+random_seed()%0A+++call+random_number(x)++!!+generate+uniform+random+deviates+on+(0,+1%5D%0A+++call+stats(x,+xmean,+xsd)%0A%0A+++print+*,+xmean,+xsd%0A%0Acontains%0A%0A+++pure+subroutine+stats(x,+xmean,+xsd)%0A++++++real,+intent(in)++::+x(:)++!!+data+for+which+stats+computed%0A++++++real,+intent(out)+::+xmean+!!+mean+of+x(:)%0A++++++real,+intent(out)+::+xsd+++!!+standard+deviation+of+x(:)%0A++++++real,+parameter+++::+bad+%3D+-999.0%0A++++++integer+++++++++++::+n%0A++++++n+%3D+size(x)%0A%0A++++++!!+Set+outputs+to+bad+value+if+there+is+insufficient+data%0A++++++xmean+%3D+bad%0A++++++xsd+%3D+bad%0A++++++if+(n+%3C+1)+return%0A++++%0A++++++!!+We+have+at+least+one+value,+enough+to+compute+mean%0A++++++xmean+%3D+sum(x)/size(x)%0A++++++if+(n+%3C+2)+return%0A%0A++++++!!+If+we+get+to+here,+we+have+at+least+two+values,%0A++++++!!+enough+data+to+compute+standard+deviation.%0A++++++xsd+%3D+sqrt(sum((x+-+xmean)**2)/(n+-+1))%0A+++end+subroutine+stats%0A%0Aend+program+optional_arg%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%20Define%20and%20call%20a%20subroutine%0Aprogram%20optional_arg%0A%20%20%20implicit%20none%0A%20%20%20real%20%3A%3A%20x%281000000%29%2C%20xmean%2C%20xsd%0A%0A%20%20%20call%20random_seed%28%29%0A%20%20%20call%20random_number%28x%29%20%20%21%20generate%20uniform%20random%20deviates%20on%20%280%2C%201%5D%0A%20%20%20call%20stats%28x%2C%20xmean%2C%20xsd%29%0A%0A%20%20%20print%20%2A%2C%20xmean%2C%20xsd%0A%0Acontains%0A%0A%20%20%20pure%20subroutine%20stats%28x%2C%20xmean%2C%20xsd%29%0A%20%20%20%20%20%20real%2C%20intent%28in%29%20%20%3A%3A%20x%28%3A%29%20%20%21%20data%20for%20which%20stats%20computed%0A%20%20%20%20%20%20real%2C%20intent%28out%29%20%3A%3A%20xmean%20%21%20mean%20of%20x%28%3A%29%0A%20%20%20%20%20%20real%2C%20intent%28out%29%20%3A%3A%20xsd%20%20%20%21%20standard%20deviation%20of%20x%28%3A%29%0A%20%20%20%20%20%20real%2C%20parameter%20%20%20%3A%3A%20bad%20%3D%20-999.0%0A%20%20%20%20%20%20integer%20%20%20%20%20%20%20%20%20%20%20%3A%3A%20n%0A%20%20%20%20%20%20n%20%3D%20size%28x%29%0A%0A%20%20%20%20%20%20%21%20Set%20outputs%20to%20bad%20value%20if%20there%20is%20insufficient%20data%0A%20%20%20%20%20%20xmean%20%3D%20bad%0A%20%20%20%20%20%20xsd%20%3D%20bad%0A%20%20%20%20%20%20if%20%28n%20%3C%201%29%20return%0A%20%20%20%20%0A%20%20%20%20%20%20%21%20We%20have%20at%20least%20one%20value%2C%20enough%20to%20compute%20mean%0A%20%20%20%20%20%20xmean%20%3D%20sum%28x%29/size%28x%29%0A%20%20%20%20%20%20if%20%28n%20%3C%202%29%20return%0A%0A%20%20%20%20%20%20%21%20If%20we%20get%20to%20here%2C%20we%20have%20at%20least%20two%20values%2C%0A%20%20%20%20%20%20%21%20enough%20data%20to%20compute%20standard%20deviation.%0A%20%20%20%20%20%20xsd%20%3D%20sqrt%28sum%28%28x%20-%20xmean%29%2A%2A2%29/%28n%20-%201%29%29%0A%20%20%20end%20subroutine%20stats%0A%0Aend%20program%20optional_arg%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]

  0.500264764      0.288482279    

```

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

Subroutines are commonly used to return multiple values,
although a function can return an array or a user-defined type that contains multiple values.
Use functions when possible, but subroutines are often needed.
The subroutine is invoked with the `call` statement.

---

<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/Wi7Imqi5vW">pic.twitter.com/Wi7Imqi5vW</a></p>&mdash; FortranTip (@fortrantip) <a href="https://twitter.com/fortrantip/status/1472692305522044932?ref_src=twsrc%5Etfw">December 19, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>