Card 03/ 10
Parameters vs Arguments
Two words used as if they were one, until an error message uses one of them precisely.
A parameter is a name in a method's declaration, a variable the method will receive. An argument is the actual value a caller hands over when calling it. Each argument fills the parameter in the same position.
The two words get used as though they meant the same thing, until an error message uses one of them precisely and stops making sense. In this listing, line 1 has the parameter and line 5 the argument:
long vatOn(long pence) {
return pence * 20 / 100;
}
long tax = vatOn(1999);| Word | Where it lives | In the listing |
|---|---|---|
| Parameter | In the declaration | pence: a name with a type, and nothing in it yet |
| Argument | At the call | 1999: an actual value, handed over |
A parameter is a local variable that the method declares and the caller fills in. It exists only while the method runs, and its name is invisible to callers: renaming pence to amount changes nothing outside the body.
The order matters and the names do not. transfer(from, to) and transfer(to, from) compile identically when both are the same type, which is why a method with three parameters of one type is a method waiting to be called wrongly.
The compiler checks each argument against its parameter's type. Passing 19.99 to vatOn fails to compile, because a double does not fit in a long parameter without a cast, a type in brackets that forces the conversion.
Holding the distinction is what makes an error like "incompatible types: String cannot be converted to int" readable: it is telling you which argument failed to fit which parameter.
← → move through sections and cards