[Edu-sig] explaining functions [Possibly OT]
Kirby Urner
urnerk at qwest.net
Mon Dec 6 01:36:25 CET 2004
Kent:
> Isn't a math function the same as a computer-science function? You
> give the math function an x, and it returns a y. ie: f(x) = 2*(4x)^x
> -or- def f(x): print 2*((4*x)**x). Most people should be able to grasp
> that, if they've been through highschool algebra.
>
Computer science functions tend to have richer syntax, plus don't always
spit out their results through the 'return' at the end. In C/C++/C# you'll
typically pass in an argument that gets worked on by the function, and is
thereby changed, with nothing explicitly returned (return type could be
void).
In Python, as everything is referential, this is easy to do as well:
>>> mylist = range(3)
>>> def additem(somelist):
somelist.append(100)
>>> additem(mylist)
>>> mylist
[0, 1, 2, 100]
Note the absence of any 'return' key word.
In mathematics, the formal idea of a function is probably best represented
by a simple Python dictionary, which is a 'mapping' -- and a 'mapping' is
what a function is. You'll always see these 'cloud' diagrams, showing a
cloud on the left (domain) and a cloud on the right (image), with arrows
going in between.
I like the map representation in that it doesn't presume there has to be a
rule. Most functions we encounter in algebra books *do* have a rule, such
that we're able to compute outputs by applying an algorithm. But the idea
of a function doesn't require such. {(1,3),(2,18),(3,-7),(8, 9.1)} is a
perfectly good example of a mapping with no apparent rhyme or reason.
The important thing about a function is the same inputs always map to the
same outputs. There's no "fork in the road" where it could go one way or
the other -- any such fork would have to pair with some additional input
variable. This, too, corresponds with the dictionary idea, in that the same
key can't map to more than one value (sure, that value might be a list, but
the same key won't point to a different list elsewhere in the dictionary --
indeed, keys must be unique).
I think the black box idea is just fine. I like to think of functions as
verbs. They "do stuff" to inputs. I think of factories with bottles
zooming by on a conveyor belt, and a machine screwing a lid onto each.
Every bottle maps to itself with a lid on it. The machine is the function
doing the mapping.
More generically, I like to think of the present moment as a domain, and the
very next moment as its image, and so on. The 'verb' is simply change, in
all its bandwidth and glory. Our experience is just one long 'function'
(events going to more events). We don't really know all the rules at work,
but certainly there's a strong sense of rules working.
Kirby
More information about the Edu-sig
mailing list