source: public/doc/gnu-c/Function-Body.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.5 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Function Body (GNU C Language Manual)</title>
23
24<meta name="description" content="Function Body (GNU C Language Manual)">
25<meta name="keywords" content="Function Body (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="Recursive-Fibonacci.html" rel="up" title="Recursive Fibonacci">
33<link href="Stack.html" rel="next" title="Stack">
34<link href="Function-Header.html" rel="prev" title="Function Header">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Function-Body"></span><div class="header">
59<p>
60Previous: <a href="Function-Header.html" accesskey="p" rel="prev">Function Header</a>, Up: <a href="Recursive-Fibonacci.html" accesskey="u" rel="up">Recursive Fibonacci</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Function-Body-1"></span><h4 class="subsection">1.1.2 Function Body</h4>
64<span id="index-function-body"></span>
65<span id="index-recursion"></span>
66
67<p>The rest of the function definition is called the <em>function body</em>.
68Like every function body, this one starts with &lsquo;<samp>{</samp>&rsquo;, ends with
69&lsquo;<samp>}</samp>&rsquo;, and contains zero or more <em>statements</em> and
70<em>declarations</em>. Statements specify actions to take, whereas
71declarations define names of variables, functions, and so on. Each
72statement and each declaration ends with a semicolon (&lsquo;<samp>;</samp>&rsquo;).
73</p>
74<p>Statements and declarations often contain <em>expressions</em>; an
75expression is a construct whose execution produces a <em>value</em> of
76some data type, but may also take actions through &ldquo;side effects&rdquo;
77that alter subsequent execution. A statement, by contrast, does not
78have a value; it affects further execution of the program only through
79the actions it takes.
80</p>
81<p>This function body contains no declarations, and just one statement,
82but that one is a complex statement in that it contains nested
83statements. This function uses two kinds of statements:
84</p>
85<dl compact="compact">
86<dt><code>return</code></dt>
87<dd><p>The <code>return</code> statement makes the function return immediately.
88It looks like this:
89</p>
90<div class="example">
91<pre class="example">return <var>value</var>;
92</pre></div>
93
94<p>Its meaning is to compute the expression <var>value</var> and exit the
95function, making it return whatever value that expression produced.
96For instance,
97</p>
98<div class="example">
99<pre class="example">return 1;
100</pre></div>
101
102<p>returns the integer 1 from the function, and
103</p>
104<div class="example">
105<pre class="example">return fib (n - 1) + fib (n - 2);
106</pre></div>
107
108<p>returns a value computed by performing two function calls
109as specified and adding their results.
110</p>
111</dd>
112<dt><code><code>if</code>&hellip;<code>else</code></code></dt>
113<dd><p>The <code>if</code>&hellip;<code>else</code> statement is a <em>conditional</em>.
114Each time it executes, it chooses one of its two substatements to execute
115and ignores the other. It looks like this:
116</p>
117<div class="example">
118<pre class="example">if (<var>condition</var>)
119 <var>if-true-statement</var>
120else
121 <var>if-false-statement</var>
122</pre></div>
123
124<p>Its meaning is to compute the expression <var>condition</var> and, if it&rsquo;s
125&ldquo;true,&rdquo; execute <var>if-true-statement</var>. Otherwise, execute
126<var>if-false-statement</var>. See <a href="if_002delse-Statement.html">if-else Statement</a>.
127</p>
128<p>Inside the <code>if</code>&hellip;<code>else</code> statement, <var>condition</var> is
129simply an expression. It&rsquo;s considered &ldquo;true&rdquo; if its value is
130nonzero. (A comparison operation, such as <code>n &lt;= 2</code>, produces the
131value 1 if it&rsquo;s &ldquo;true&rdquo; and 0 if it&rsquo;s &ldquo;false.&rdquo; See <a href="Numeric-Comparisons.html">Numeric Comparisons</a>.) Thus,
132</p>
133<div class="example">
134<pre class="example">if (n &lt;= 2)
135 return 1;
136else
137 return fib (n - 1) + fib (n - 2);
138</pre></div>
139
140<p>first tests whether the value of <code>n</code> is less than or equal to 2.
141If so, the expression <code>n &lt;= 2</code> has the value 1. So execution
142continues with the statement
143</p>
144<div class="example">
145<pre class="example">return 1;
146</pre></div>
147
148<p>Otherwise, execution continues with this statement:
149</p>
150<div class="example">
151<pre class="example">return fib (n - 1) + fib (n - 2);
152</pre></div>
153
154<p>Each of these statements ends the execution of the function and
155provides a value for it to return. See <a href="return-Statement.html">return Statement</a>.
156</p></dd>
157</dl>
158
159<p>Calculating <code>fib</code> using ordinary integers in C works only for
160<var>n</var> &lt; 47, because the value of <code>fib (47)</code> is too large to fit
161in type <code>int</code>. The addition operation that tries to add
162<code>fib (46)</code> and <code>fib (45)</code> cannot deliver the correct result.
163This occurrence is called <em>integer overflow</em>.
164</p>
165<p>Overflow can manifest itself in various ways, but one thing that can&rsquo;t
166possibly happen is to produce the correct value, since that can&rsquo;t fit
167in the space for the value. See <a href="Integer-Overflow.html">Integer Overflow</a>.
168</p>
169<p>See <a href="Functions.html">Functions</a>, for a full explanation about functions.
170</p>
171<hr>
172<div class="header">
173<p>
174Previous: <a href="Function-Header.html" accesskey="p" rel="prev">Function Header</a>, Up: <a href="Recursive-Fibonacci.html" accesskey="u" rel="up">Recursive Fibonacci</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
175</div>
176
177
178
179</body>
180</html>
Note: See TracBrowser for help on using the repository browser.