Nick Keighley said:
I was trying to recall my first programming language. Were parameters
difficult to understand then? I don't think so.
I don't think they're particularly difficult, but I remember working in
the lab at school and answering lots of questions which were the result
of basic misunderstandings of how parameters and arguments worked.
Clarification didn't take long, and 99% of people would "get" it, but
I've wondered where the confusion came from in the first place.
My background before C was BASIC, Assembly, and Pascal. (Assembly
before C was awesome; in a perfect world, I think I'd like to teach
people computer science from the ground up. In a money-driven world, we
start with Java or Python.
With Pascal, you could use the "var" keyword in your parameter list to
affect the argument from within the function (a "reference"? I don't
know the exact mechanism) and there was no corresponding simple way to
do that in C.
If my background had included Java or Python before C, I might find C
more confusing, but I'm not sure. More investigation is probably
warranted to see if this is true.
I think I thought because variables local to a function (and I included
parameters in the class of variables local to a function) only appeared
in the code once textually (lexically) speaking- then there was only a
single instance of them.
Hmmm! I think this is an excellent observation. And the simple 1-2-3 I
had posted earlier doesn't really suggest that a new copy of the
parameters is made each call. (A reader might question, "When you call
it again, what happens to the parameters? Do they get overwritten?")
But why initialisation rather tha assignment would fix this conceptual
error I'm not sure. I suppose I was thinking (this is the current me
not the historical one) "initialise the variable" true attention to the
fact that a brand new variable was being created.
I know the spec doesn't say anything about a stack. But it does say:
* Recursive calls are permitted (C99 6.5.2.2p11)
* Parameters are effectively declared at the top of the function body
block (C99 6.9.1p9)
* If a block containing an object is entered recursively, a new instance
of the object is created each time (C99 6.2.4p5-6)
It seems like there should be a way to present it very concisely in a
way a beginner would fully comprehend.
Trying to think like a new programmer is hard!
Yeah it is. When I mentioned teaching CSCI from the ground up earlier,
part of my reasoning is that people would get a foundation on
which to attach new knowledge. When they start with Java, it's just
like starting in free-floating space. Above the new programmers is all
kinds of stuff they don't know, and below them is all kinds of stuff
they don't know, and I worry that lack of context impedes their
learning. The mysterious stuff all around them can sometimes make for
strange assumptions.
When I was a a I'm sure the set of possible misconceptions is gigantic.
At least as large as the set of proper conceptions.
I used not to put blank lines to save disk space (I assumed a blank
line took a whole line's worth of characters (80 I suppose))
When I was a kid first starting with BASIC, I remember wondering what
all these "REM" statements were. I speculated that they didn't have an
effect, but in order to be sure, I put a new REM statement in my code
that ordered my spaceship to fly sideways instead of upward. It didn't
work.
I felt sheepish. I still feel sheepish.
But these are the sorts of actual amazing misconceptions people can
have!
-Beej