source: public/doc/gnu-c/Recursive-Fibonacci.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: 6.7 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>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<!--
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="Recursive-Fibonacci"></span><div class="header">
59<p>
60Next: <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> &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="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&rsquo;s look at code for a
68simple mathematical function that does calculations on integers. This
69function calculates the <var>n</var>th number in the Fibonacci series, in
70which each number is the sum of the previous two: 1, 1, 2, 3, 5, 8,
7113, 21, 34, 55, &hellip;.
72</p>
73<div class="example">
74<pre class="example">int
75fib (int n)
76{
77 if (n &lt;= 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
88header. 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>
91inside the function body. See <a href="Function-Parameter-Variables.html">Function Parameter Variables</a>.
92A function definition uses parameters to refer to the argument
93values provided in a call to that function.
94
95</li><li> Arithmetic. C programs add with &lsquo;<samp>+</samp>&rsquo; and subtract with
96&lsquo;<samp>-</samp>&rsquo;. See <a href="Arithmetic.html">Arithmetic</a>.
97
98</li><li> Numeric comparisons. The operator &lsquo;<samp>&lt;=</samp>&rsquo; tests for &ldquo;less than or
99equal.&rdquo; See <a href="Numeric-Comparisons.html">Numeric Comparisons</a>.
100
101</li><li> Integer constants written in base 10.
102See <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
105function <code>fib</code>, passing as its argument the value <code>n - 1</code>.
106See <a href="Function-Calls.html">Function Calls</a>.
107
108</li><li> A comment, which starts with &lsquo;<samp>/*</samp>&rsquo; and ends with &lsquo;<samp>*/</samp>&rsquo;. The
109comment has no effect on the execution of the program. Its purpose is
110to provide explanations to people reading the source code. Including
111comments in the code is tremendously important&mdash;they provide
112background information so others can understand the code more quickly.
113See <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>&hellip;<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&rsquo;t return.
122Thus, recursive definitions, to be of any use, must avoid infinite
123recursion.
124</p>
125<p>This function definition prevents infinite recursion by specially
126handling the case where <code>n</code> is two or less. Thus the maximum
127depth 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">&bull; <a href="Function-Header.html" accesskey="1">Function Header</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">The function&rsquo;s name and how it is called.
132</td></tr>
133<tr><td align="left" valign="top">&bull; <a href="Function-Body.html" accesskey="2">Function Body</a></td><td>&nbsp;&nbsp;</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>
140Next: <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> &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>
141</div>
142
143
144
145</body>
146</html>
Note: See TracBrowser for help on using the repository browser.