# <span class='text-muted'>048.</span> `cmplx` should be used with a `kind` argument

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

[`cmplx(x, y [, kind])`](https://gcc.gnu.org/onlinedocs/gfortran/CMPLX.html)
returns a single precision complex variable,
even if `x` and `y` are double precision, unless a `kind` is specified.

```{literalinclude} ../../src/cmplx.f90
:language: fortran
:caption: cmplx.f90 | <a href="https://github.com/zmoon/FortranTipBrowser/blob/main/src/cmplx.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/cmplx.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:33,positionColumn:1,positionLineNumber:33,selectionStartColumn:1,selectionStartLineNumber:33,startColumn:1,startLineNumber:33),source:'program+test_cmplx%0A+++implicit+none%0A%0A+++integer,+parameter+::+dp+%3D+kind(0.0d0)%0A%0A+++complex,+parameter+::+c1+%3D+(1.0,+1.0)%0A+++complex,+parameter+::+c2+%3D+(1.0d0,+1.0d0)%0A+++complex(kind%3Ddp),+parameter+::+c3+%3D+(1.0d0,+1.0d0)%0A%0A+++!!+Parameter%0A+++print+*,+kind(c1)++!!+4%0A+++print+*,+kind(c2)++!!+4%0A+++print+*,+kind(c3)++!!+8%0A+++print+*%0A%0A+++!!+Inline+literal%0A+++print+*,+kind((1.0,+1.0))++++++!!+4%0A+++print+*,+kind((1.0,+1.0d0))++++!!+8%0A+++print+*,+kind((1.0d0,+1.0))++++!!+8%0A+++print+*,+kind((1.0d0,+1.0d0))++!!+8%0A+++print+*%0A%0A+++!!+Calling+%60cmplx%60%0A+++print+*,+kind(cmplx(1.0d0,+1.0d0))++!!+4+--+likely+unintended!!%0A+++print+*,+kind(cmplx(1.0d0,+1.0d0,+kind%3Ddp))++!!+8%0A+++print+*%0A%0A+++!!+Same+if+passing+real+component+only%0A+++print+*,+kind(cmplx(1.0d0))++!!+4%0A+++print+*,+kind(cmplx(1.0d0,+kind%3Ddp))++!!+8%0A+++print+*%0A%0Aend+program+test_cmplx%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=program%20test_cmplx%0A%20%20%20implicit%20none%0A%0A%20%20%20integer%2C%20parameter%20%3A%3A%20dp%20%3D%20kind%280.0d0%29%0A%0A%20%20%20complex%2C%20parameter%20%3A%3A%20c1%20%3D%20%281.0%2C%201.0%29%0A%20%20%20complex%2C%20parameter%20%3A%3A%20c2%20%3D%20%281.0d0%2C%201.0d0%29%0A%20%20%20complex%28kind%3Ddp%29%2C%20parameter%20%3A%3A%20c3%20%3D%20%281.0d0%2C%201.0d0%29%0A%0A%20%20%20%21%20Parameter%0A%20%20%20print%20%2A%2C%20kind%28c1%29%20%20%21%204%0A%20%20%20print%20%2A%2C%20kind%28c2%29%20%20%21%204%0A%20%20%20print%20%2A%2C%20kind%28c3%29%20%20%21%208%0A%20%20%20print%20%2A%0A%0A%20%20%20%21%20Inline%20literal%0A%20%20%20print%20%2A%2C%20kind%28%281.0%2C%201.0%29%29%20%20%20%20%20%20%21%204%0A%20%20%20print%20%2A%2C%20kind%28%281.0%2C%201.0d0%29%29%20%20%20%20%21%208%0A%20%20%20print%20%2A%2C%20kind%28%281.0d0%2C%201.0%29%29%20%20%20%20%21%208%0A%20%20%20print%20%2A%2C%20kind%28%281.0d0%2C%201.0d0%29%29%20%20%21%208%0A%20%20%20print%20%2A%0A%0A%20%20%20%21%20Calling%20%60cmplx%60%0A%20%20%20print%20%2A%2C%20kind%28cmplx%281.0d0%2C%201.0d0%29%29%20%20%21%204%20--%20likely%20unintended%21%0A%20%20%20print%20%2A%2C%20kind%28cmplx%281.0d0%2C%201.0d0%2C%20kind%3Ddp%29%29%20%20%21%208%0A%20%20%20print%20%2A%0A%0A%20%20%20%21%20Same%20if%20passing%20real%20component%20only%0A%20%20%20print%20%2A%2C%20kind%28cmplx%281.0d0%29%29%20%20%21%204%0A%20%20%20print%20%2A%2C%20kind%28cmplx%281.0d0%2C%20kind%3Ddp%29%29%20%20%21%208%0A%20%20%20print%20%2A%0A%0Aend%20program%20test_cmplx%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]

           4
           4
           8

           4
           8
           8
           8

           4
           8

           4
           8


```

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

Some further references:
* <https://fortran-lang.discourse.group/t/complex-constants-and-variables/2909>

---

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">CMPLX(X,Y,[KIND]) returns a single precision complex variable, even if X and Y are double precision, unless a KIND is specified. <br><br>Merry Christmas and Happy Holidays! Posts may be sparse until the new year. <a href="https://t.co/MD9tCw4pTB">pic.twitter.com/MD9tCw4pTB</a></p>&mdash; FortranTip (@fortrantip) <a href="https://twitter.com/fortrantip/status/1474537984066301959?ref_src=twsrc%5Etfw">December 25, 2021</a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>