Re: Malcolm's new book



Eric Sosman <Eric.Sosman@xxxxxxx> writes:
mstorkamp@xxxxxxxxx wrote On 07/25/07 10:56,:
[...]
It may be I've actually leaned something new from the sample pages of
the book. To quote:

Note that strlen() is a "pure function". It performs no input or
output, and all the information it requires is contained in the
parameters. One of the features of C is that there is no distinction
between a "function", which calculates something, and a "procedure",
which performs IO.

He says there's no distinction, tries to draw one,
and ends up spreading confusion.

Now I had always had in my mind (going back to the days of learning
BASIC in the '70s) that the difference between a function and
procedure is that a function returns a value and a procedure doesn't.
But I could be wrong.

Different programming languages use different terms
to describe various flavors of functions, subroutines,
procedures, methods, lambda expressions, and so on. When
you're dealing with definitional niceties, you need to do
so in the context of a particular language; the next in
line may use the same word but with a different meaning.

In the specific case of C, "procedure" is a word with
no meaning. The Standard does not define it; the Standard
does not even use the word. So there's no way for C to
clear up any confusion you may have about the precise meaning
of "procedure;" one might just as well ask about the precise
meaning of "coroutine."

By the way, "pure function" is another phrase not defined
or used by the C Standard. The term is in general use, with
a meaning somewhat along the lines Malcolm mentions: most
people describe a function as "pure" if it has no "visible"
side-effects. Section 7.5 paragraph 3 of the C Standard
explicitly permits strlen() to have an externally-visible
side-effect, so strlen() need not be "pure" at all!

I don't have too much of a problem with a C book defining and using
terms like "procedure" and "pure function", as long as it (a) defines
them clearly, (b) defines them in ways that don't conflict too badly
with common usage, and (c) make it very clear that the terms are being
defined by the book, not by the C standard.

It could also be argued that a C book should just use the terminology
defined by the standard, but Malcolm's book isn't really *about* C;
it's primarily about algorithms, using C to present them. In that
context, distinguishing "procedures" and "pure functions" from other
functions is probably sensible.

<OT>
An early draft version of Ada (from 1979) had "procedures" that
perform actions without returning a value, "value-returning
procedures" that perform actions and return a value, and "functions"
that return a value and are tightly restricted in their side effects.
For example, you couldn't add I/O statements to a function. In later
revisions, value-returning procedures were dropped, and the
restrictions on functions were loosened.
</OT>

But I've never heard of the term "procedure" being defined in terms of
I/O. Normally a "procedure" would be what C calls a function
returning void. A lot of C functions also have a primary purpose of
causing side effects, but return a result that indicates whether it
succeeded or not; some other languages might, for example, use a
procedure that raises an exception on failure. (qsort() is a good
example.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Malcolms new book
    ... Note that strlenis a "pure function". ... line may use the same word but with a different meaning. ... The Standard does not define it; ... side-effects. ...
    (comp.lang.c)
  • Re: Malcolms new book
    ... Different programming languages use different terms ... line may use the same word but with a different meaning. ... The Standard does not define it; ... Here's a quote from chapter one: ...
    (comp.lang.c)
  • Re: The annotated annotated annotated C standard part 2
    ... A nice sounding statement, but totally meaningless. ... the body of a function is a compound ... Of course, the C Standard uses "block" to mean exactly the same thing as "compound statement", so Schildt's statement makes even less sense if I take your interpretation. ... Algol meaning, a profoundly important meaning which acknowledges the ...
    (comp.programming)
  • Re: function return pointer of int?
    ... The standard uses "value" to mean different things in ... the meaning is always as defined. ... It produces a value of pointer type. ... then it may be a trap representation, which is not a value of pointer ...
    (comp.lang.c)
  • Re: detabbing again
    ... The standard exists independent of any observer, ... (again in my opinion) ... meaning of some previous statements. ...
    (comp.lang.c)