Teacher Note: Names for Formal and Actual Parameters¶
A formal parameter is the name of the input value to a procedure, while
an actual parameter is the value itself that is passed to the procedure
when it is called. In the following example,
name is the formal
"John" is the actual parameter:
Of course, instead of passing the literal string
"John", we can
alternatively pass a variable name:
In this case,
name is the formal parameter, while
person is the
Depending on the examples of procedure definitions and calls the students encounter, some may think that it is important to the computer whether or not the names of the formal and actual parameters match.
Correct understanding In fact, it does not matter if the names do
not match, as we see in the example above. Making use of our
variables-as-boxes analogy, what is actually passed to the procedure is
a copy of the value contained in the box, not the box itself. For
both calls to
greet above —
— the same value is passed:
Run the above code in CodeLens to see that the value
copied to the variable
name from the variable
Conversely, the following example demonstrates that you can also use the same names for the formal and actual parameters:
This example might seem a bit more confusing, since we use
twice. We understand, however, that the name of the actual parameter
does not matter, since it is a copy of the value that we are passing.
This code, then, will behave exactly as the previous two examples.
How do we make sense then of
name being used twice? If you run this
example in CodeLens, you’ll see that what actually happens is that there
name variables. When
greet is called, the value of
name in the Global frame is copied into the variable
the greet frame.
How to tell if a student has this misconception Show all three of the above examples, and ask the students if any of them would produce an error.