section 1.7: Functions

page 24

Deep sentence:

...you will often see a short function defined and called only once, just because it clarifies some piece of code.
Ideally, this is true in any language. Breaking a program up into functions (or subroutines or procedures or whatever a language calls them) is one of the first and one of the most important ways to keep control of the proliferating complexity in a software project.

page 25

Note that the for loop at the top of the page runs from 1 to n rather than 0 to n-1, and may therefore seem suspect by the above note for page 22. In this case, since all that matters is that the loop is traversed n times, it doesn't matter which values i takes on.

Not only the names of the parameters and local variables, but also their values (as we'll see in section 1.8), are all local to a function. Rather than remembering a list of things that are local, it's easier to remember that everything is local: the whole point of a function as an abstraction mechanism is that it's a black box; you don't have to know or care about any of its implementation details, such as what it chooses to name its parameters and local variables. You pass it some arguments, and it returns you a value according to its specification.

The distinction between the terms argument and parameter may seem overly picky, but it's a good way of reinforcing the notion that the parameters and other details of a function's implementation are almost completely separated from (that is, of no concern to) the caller.

page 26

Note the discussion about return values from main. The first few sample programs in this chapter, including the very first ``hello, world'' example on page 6, have omitted a return value, which is, stricly speaking, incorrect. Do get in the habit of returning a value from main, both to be correct, and because ``programs should return status to their environment.''

By ``Parameter names need not agree'' they mean that it's not a problem that the prototype declaration of power says that the first parameter is named m, while the actual function definition that it's named base.

pages 26-7

It's probably a good idea if you're aware of this ``old style'' function syntax, so that you won't be taken aback when you come across it, perhaps in code written by reactionary old fogies (such as the author of these notes) who still tend to use it out of habit when they're not paying attention.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback