Its a conceptual question. But I would like to use the right term at the right place. That is why I would like to read some other views on this.
2
In layman’s words:
- Imagine a function as a black box.
- You don’t know how the black box works
- You only know what the black box does, but not how
- The black box needs something to work on, that you must provide
- The black box gives you something in return
- That thing you provide the black box is a parameter
- The pieces of information you don’t know of, that the black box uses to do it’s work, are variables
- But hey!, for you (the caller), the thing you give the black box can be a variable of your own, that you use to do your own work.
- You can also give the black box a literal, i.e, instead of passing it the variable
personName
you could pass it “Peter”. The black box doesn’t know whether you passed it a variable or a literal. Once inside the black box, it’s a variable as seen from the perspective of the black box. - Finally the black box gives you something back, which is called a “return value”, which you can put in a variable or your own, or not, or you can pass to another box as a parameter.
- Some black boxes don’t need you to pass them anything.
- Some black boxes don’t return anything, and people call them “methods”.
- Some people call parameters “arguments”.
0
Parameters are variables that only exist within a method, and are initialized automatically with the values passed to the method.
Parameters can be used as a special type of variable, but the basic difference is scope: that a variable is local (it only exists within the current method), whereas a parameter is external: it is passed in to the method from the caller. This means that, if the parameter is passed by reference (including essentially any object type), changes to the parameter will be reflected in the calling code once the method exits, whereas anything done to a local variable does not live beyond the method. (Unless it gets passed elsewhere and stored, of course.)
3