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
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing 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>Recursive Fibonacci (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Recursive Fibonacci (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Recursive Fibonacci (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="The-First-Example.html" rel="up" title="The First Example">
|
---|
33 | <link href="Function-Header.html" rel="next" title="Function Header">
|
---|
34 | <link href="The-First-Example.html" rel="prev" title="The First Example">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Recursive-Fibonacci"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Stack.html" accesskey="n" rel="next">Stack</a>, Up: <a href="The-First-Example.html" accesskey="u" rel="up">The First Example</a> [<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="Example_003a-Recursive-Fibonacci"></span><h3 class="section">1.1 Example: Recursive Fibonacci</h3>
|
---|
64 | <span id="index-recursive-Fibonacci-function"></span>
|
---|
65 | <span id="index-Fibonacci-function_002c-recursive"></span>
|
---|
66 |
|
---|
67 | <p>To introduce the most basic features of C, let’s look at code for a
|
---|
68 | simple mathematical function that does calculations on integers. This
|
---|
69 | function calculates the <var>n</var>th number in the Fibonacci series, in
|
---|
70 | which each number is the sum of the previous two: 1, 1, 2, 3, 5, 8,
|
---|
71 | 13, 21, 34, 55, ….
|
---|
72 | </p>
|
---|
73 | <div class="example">
|
---|
74 | <pre class="example">int
|
---|
75 | fib (int n)
|
---|
76 | {
|
---|
77 | if (n <= 2) /* <span class="roman">This avoids infinite recursion.</span> */
|
---|
78 | return 1;
|
---|
79 | else
|
---|
80 | return fib (n - 1) + fib (n - 2);
|
---|
81 | }
|
---|
82 | </pre></div>
|
---|
83 |
|
---|
84 | <p>This very simple program illustrates several features of C:
|
---|
85 | </p>
|
---|
86 | <ul>
|
---|
87 | <li> A function definition, whose first two lines constitute the function
|
---|
88 | header. See <a href="Function-Definitions.html">Function Definitions</a>.
|
---|
89 |
|
---|
90 | </li><li> A function parameter <code>n</code>, referred to as the variable <code>n</code>
|
---|
91 | inside the function body. See <a href="Function-Parameter-Variables.html">Function Parameter Variables</a>.
|
---|
92 | A function definition uses parameters to refer to the argument
|
---|
93 | values provided in a call to that function.
|
---|
94 |
|
---|
95 | </li><li> Arithmetic. C programs add with ‘<samp>+</samp>’ and subtract with
|
---|
96 | ‘<samp>-</samp>’. See <a href="Arithmetic.html">Arithmetic</a>.
|
---|
97 |
|
---|
98 | </li><li> Numeric comparisons. The operator ‘<samp><=</samp>’ tests for “less than or
|
---|
99 | equal.” See <a href="Numeric-Comparisons.html">Numeric Comparisons</a>.
|
---|
100 |
|
---|
101 | </li><li> Integer constants written in base 10.
|
---|
102 | See <a href="Integer-Constants.html">Integer Constants</a>.
|
---|
103 |
|
---|
104 | </li><li> A function call. The function call <code>fib (n - 1)</code> calls the
|
---|
105 | function <code>fib</code>, passing as its argument the value <code>n - 1</code>.
|
---|
106 | See <a href="Function-Calls.html">Function Calls</a>.
|
---|
107 |
|
---|
108 | </li><li> A comment, which starts with ‘<samp>/*</samp>’ and ends with ‘<samp>*/</samp>’. The
|
---|
109 | comment has no effect on the execution of the program. Its purpose is
|
---|
110 | to provide explanations to people reading the source code. Including
|
---|
111 | comments in the code is tremendously important—they provide
|
---|
112 | background information so others can understand the code more quickly.
|
---|
113 | See <a href="Comments.html">Comments</a>.
|
---|
114 |
|
---|
115 | </li><li> Two kinds of statements, the <code>return</code> statement and the
|
---|
116 | <code>if</code>…<code>else</code> statement. See <a href="Statements.html">Statements</a>.
|
---|
117 |
|
---|
118 | </li><li> Recursion. The function <code>fib</code> calls itself; that is called a
|
---|
119 | <em>recursive call</em>. These are valid in C, and quite common.
|
---|
120 |
|
---|
121 | <p>The <code>fib</code> function would not be useful if it didn’t return.
|
---|
122 | Thus, recursive definitions, to be of any use, must avoid infinite
|
---|
123 | recursion.
|
---|
124 | </p>
|
---|
125 | <p>This function definition prevents infinite recursion by specially
|
---|
126 | handling the case where <code>n</code> is two or less. Thus the maximum
|
---|
127 | depth of recursive calls is less than <code>n</code>.
|
---|
128 | </p></li></ul>
|
---|
129 |
|
---|
130 | <table class="menu" border="0" cellspacing="0">
|
---|
131 | <tr><td align="left" valign="top">• <a href="Function-Header.html" accesskey="1">Function Header</a></td><td> </td><td align="left" valign="top">The function’s name and how it is called.
|
---|
132 | </td></tr>
|
---|
133 | <tr><td align="left" valign="top">• <a href="Function-Body.html" accesskey="2">Function Body</a></td><td> </td><td align="left" valign="top">Declarations and statements that implement the function.
|
---|
134 | </td></tr>
|
---|
135 | </table>
|
---|
136 |
|
---|
137 | <hr>
|
---|
138 | <div class="header">
|
---|
139 | <p>
|
---|
140 | Next: <a href="Stack.html" accesskey="n" rel="next">Stack</a>, Up: <a href="The-First-Example.html" accesskey="u" rel="up">The First Example</a> [<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>
|
---|
141 | </div>
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 | </body>
|
---|
146 | </html>
|
---|