1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>section
1.7: Functions
</title>
10 <link href=
"sx4j.html" rev=precedes
>
11 <link href=
"sx4l.html" rel=precedes
>
12 <link href=
"sx4.html" rev=subdocument
>
15 <H2>section
1.7: Functions
</H2>
19 <blockquote>...you will often see a short function defined and called only once,
20 just because it clarifies some piece of code.
21 </blockquote>Ideally, this is true in any language.
22 Breaking a program up into functions
23 (or subroutines or procedures or whatever a language calls them)
24 is one of the first and one of the most important ways
25 to keep control of the proliferating complexity in a software
28 </p><p>Note that the
<TT>for
</TT> loop at the top of the page runs from
29 <TT>1</TT> to
<TT>n
</TT> rather than
<TT>0</TT> to
<TT>n-
1</TT>,
30 and may therefore seem suspect by the above note for page
22.
31 In this case, since all that matters is that the loop is
32 traversed
<TT>n
</TT> times,
33 it doesn't matter which values
<TT>i
</TT> takes on.
34 </p><p>Not only the names of the parameters and local variables,
36 (as we'll see in section
1.8),
37 are all local to a function.
38 Rather than remembering a list of things that are local,
39 it's easier to remember that
<em>everything
</em> is local:
40 the whole point of a function as an abstraction mechanism is
41 that it's a black box; you don't have to know or care about
42 any of its implementation details,
43 such as what it chooses to name its parameters and local
45 You pass it some arguments, and it returns you a value
46 according to its specification.
47 </p><p>The distinction between the terms
<dfn>argument
</dfn> and
48 <dfn>parameter
</dfn> may seem overly picky, but it's a good way of
49 reinforcing the notion that the parameters and other details of
50 a function's implementation are almost completely separated from
51 (that is, of no concern to)
54 </p><p>Note the discussion about return values from
<TT>main
</TT>.
55 The first few sample programs in this chapter,
56 including the very first ``hello, world'' example on page
6,
57 have omitted a return value,
58 which is, stricly speaking, incorrect.
59 Do get in the habit of returning a value from
<TT>main
</TT>,
62 ``programs should return status to their environment.''
63 </p><p>By ``Parameter names need not agree''
64 they mean that it's not a problem that the prototype
65 declaration of
<TT>power
</TT> says that the first parameter is
67 while the actual function definition that it's named
<TT>base
</TT>.
69 </p><p>It's probably a good idea if you're aware of this ``old style''
71 so that you won't be taken aback when you come across it,
72 perhaps in code written by
73 reactionary old fogies
74 (such as the author of these notes)
75 who still tend to use it out of habit when they're not paying attention.
79 <a href=
"sx4j.html" rev=precedes
>prev
</a>
80 <a href=
"sx4l.html" rel=precedes
>next
</a>
81 <a href=
"sx4.html" rev=subdocument
>up
</a>
82 <a href=
"top.html">top
</a>
85 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
86 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
87 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>