Argument-passing-conventions.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1999-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The GNU Fortran Compiler: Argument passing conventions</title>
  20. <meta name="description" content="The GNU Fortran Compiler: Argument passing conventions">
  21. <meta name="keywords" content="The GNU Fortran Compiler: Argument passing conventions">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Naming-and-argument_002dpassing-conventions.html#Naming-and-argument_002dpassing-conventions" rel="up" title="Naming and argument-passing conventions">
  30. <link href="Coarray-Programming.html#Coarray-Programming" rel="next" title="Coarray Programming">
  31. <link href="Naming-conventions.html#Naming-conventions" rel="prev" title="Naming conventions">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Argument-passing-conventions"></a>
  63. <div class="header">
  64. <p>
  65. Previous: <a href="Naming-conventions.html#Naming-conventions" accesskey="p" rel="prev">Naming conventions</a>, Up: <a href="Naming-and-argument_002dpassing-conventions.html#Naming-and-argument_002dpassing-conventions" accesskey="u" rel="up">Naming and argument-passing conventions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Argument-passing-conventions-1"></a>
  69. <h4 class="subsection">7.4.2 Argument passing conventions</h4>
  70. <p>Subroutines do not return a value (matching C99&rsquo;s <code>void</code>) while
  71. functions either return a value as specified in the platform ABI or
  72. the result variable is passed as hidden argument to the function and
  73. no result is returned. A hidden result variable is used when the
  74. result variable is an array or of type <code>CHARACTER</code>.
  75. </p>
  76. <p>Arguments are passed according to the platform ABI. In particular,
  77. complex arguments might not be compatible to a struct with two real
  78. components for the real and imaginary part. The argument passing
  79. matches the one of C99&rsquo;s <code>_Complex</code>. Functions with scalar
  80. complex result variables return their value and do not use a
  81. by-reference argument. Note that with the <samp>-ff2c</samp> option,
  82. the argument passing is modified and no longer completely matches
  83. the platform ABI. Some other Fortran compilers use <code>f2c</code>
  84. semantic by default; this might cause problems with
  85. interoperablility.
  86. </p>
  87. <p>GNU Fortran passes most arguments by reference, i.e. by passing a
  88. pointer to the data. Note that the compiler might use a temporary
  89. variable into which the actual argument has been copied, if required
  90. semantically (copy-in/copy-out).
  91. </p>
  92. <p>For arguments with <code>ALLOCATABLE</code> and <code>POINTER</code>
  93. attribute (including procedure pointers), a pointer to the pointer
  94. is passed such that the pointer address can be modified in the
  95. procedure.
  96. </p>
  97. <p>For dummy arguments with the <code>VALUE</code> attribute: Scalar arguments
  98. of the type <code>INTEGER</code>, <code>LOGICAL</code>, <code>REAL</code> and
  99. <code>COMPLEX</code> are passed by value according to the platform ABI.
  100. (As vendor extension and not recommended, using <code>%VAL()</code> in the
  101. call to a procedure has the same effect.) For <code>TYPE(C_PTR)</code> and
  102. procedure pointers, the pointer itself is passed such that it can be
  103. modified without affecting the caller.
  104. </p>
  105. <p>For Boolean (<code>LOGICAL</code>) arguments, please note that GCC expects
  106. only the integer value 0 and 1. If a GNU Fortran <code>LOGICAL</code>
  107. variable contains another integer value, the result is undefined.
  108. As some other Fortran compilers use <em>-1</em> for <code>.TRUE.</code>,
  109. extra care has to be taken &ndash; such as passing the value as
  110. <code>INTEGER</code>. (The same value restriction also applies to other
  111. front ends of GCC, e.g. to GCC&rsquo;s C99 compiler for <code>_Bool</code>
  112. or GCC&rsquo;s Ada compiler for <code>Boolean</code>.)
  113. </p>
  114. <p>For arguments of <code>CHARACTER</code> type, the character length is passed
  115. as hidden argument. For deferred-length strings, the value is passed
  116. by reference, otherwise by value. The character length has the type
  117. <code>INTEGER(kind=4)</code>. Note with C binding, <code>CHARACTER(len=1)</code>
  118. result variables are returned according to the platform ABI and no
  119. hidden length argument is used for dummy arguments; with <code>VALUE</code>,
  120. those variables are passed by value.
  121. </p>
  122. <p>For <code>OPTIONAL</code> dummy arguments, an absent argument is denoted
  123. by a NULL pointer, except for scalar dummy arguments of type
  124. <code>INTEGER</code>, <code>LOGICAL</code>, <code>REAL</code> and <code>COMPLEX</code>
  125. which have the <code>VALUE</code> attribute. For those, a hidden Boolean
  126. argument (<code>logical(kind=C_bool),value</code>) is used to indicate
  127. whether the argument is present.
  128. </p>
  129. <p>Arguments which are assumed-shape, assumed-rank or deferred-rank
  130. arrays or, with <samp>-fcoarray=lib</samp>, allocatable scalar coarrays use
  131. an array descriptor. All other arrays pass the address of the
  132. first element of the array. With <samp>-fcoarray=lib</samp>, the token
  133. and the offset belonging to nonallocatable coarrays dummy arguments
  134. are passed as hidden argument along the character length hidden
  135. arguments. The token is an oparque pointer identifying the coarray
  136. and the offset is a passed-by-value integer of kind <code>C_PTRDIFF_T</code>,
  137. denoting the byte offset between the base address of the coarray and
  138. the passed scalar or first element of the passed array.
  139. </p>
  140. <p>The arguments are passed in the following order
  141. </p><ul>
  142. <li> Result variable, when the function result is passed by reference
  143. </li><li> Character length of the function result, if it is a of type
  144. <code>CHARACTER</code> and no C binding is used
  145. </li><li> The arguments in the order in which they appear in the Fortran
  146. declaration
  147. </li><li> The the present status for optional arguments with value attribute,
  148. which are internally passed by value
  149. </li><li> The character length and/or coarray token and offset for the first
  150. argument which is a <code>CHARACTER</code> or a nonallocatable coarray dummy
  151. argument, followed by the hidden arguments of the next dummy argument
  152. of such a type
  153. </li></ul>
  154. <hr>
  155. <div class="header">
  156. <p>
  157. Previous: <a href="Naming-conventions.html#Naming-conventions" accesskey="p" rel="prev">Naming conventions</a>, Up: <a href="Naming-and-argument_002dpassing-conventions.html#Naming-and-argument_002dpassing-conventions" accesskey="u" rel="up">Naming and argument-passing conventions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  158. </div>
  159. </body>
  160. </html>