When you do
function funcParm(){
$foo = "parâmetro";
echo "$foo";
}
In practice you are doing a procedure rather than a function , although it is declared as such.
What is the reason for having a procedure?
Avoid repetitions or at least organize a set of instructions contained in a place giving a name to this. As this name may refer to it somewhere in the code by name and what is in there will be executed.
If it is to be used only once the utility is reduced, but it may still be useful to give a separate responsibility, to better document that those instructions are part of one thing. In abstract examples this is not always clear.
If you are going to use the same code more than once, there is the obvious advantage of not having to repeat what has already been written and you may be doing something else DRY , which is a desirable feature of elegant code .
The big difference for a function is that it should return a value. No problem calling this a function, everyone understands, but formally it is not quite a function.
Note that in the example the variable is totally unnecessary. But anyway it is local, it is not a parameter, its value is known within the procedure and although it can vary, it will only do it inside it, it will not have a value that comes from outside.
What is parameterization?
There are cases that every time you call it, want that set of instructions to be executed, but a small portion of it is a bit different. That is, there is a gap in it that must be fulfilled in each execution. It is a variable part of the code, precisely because it varies in each call. Moreover, when you call the procedure it will tell you what that variable part is. So what we do is parametrize the function.
Parameter is just a variable that will define something.
pa · râ 'me · tro (para- + -metro) masculine noun
[Geometry] Constant line that enters the equation or construction of a curve, and serves as a fixed measure to compare the ordinates and
.abscissas.
Character or variable that allows you to define or compare something.
"parameter", in Dictionary Priberam da Língua Portuguesa [online],
2008-2013, link [consulted at
05-01-2017].
Then when you call the procedure an argument is passed, which is a value that will be assigned to the variable that serves as a parameter. To help those who are coming here and do not know What is the difference between parameter and argument? .
This gives you flexibility in using the code. This example above will always print the same thing. Already:
function funcParm2($foo){
echo "$foo";
}
funcParm("parâmetro");
You are printing this in this example. If you call as:
funcParm("outra coisa");
will print "something else" literally :) Now this procedure is more useful because it serves to solve the same problem with different information on each call whenever needed.
JavaScript Example
function send(name) { //recebe um parâmetro que não deixa de ser uma variável
return function () { //ainda estamos dentro da outra função
sendHi(name); //aquela variável é acessível aqui
}
}
//este é um caso diferenteé uma variável que se mantêm dentro do escopo
function sendHi(msg) {
console.log('Hello ' + msg);
}
var func = send('Bill'); //aqui a variável recebe como valor uma função
func(); //chama a variável como a função
sendHi('Pete'); // //chama diretamente a outra função
func();
Here is a question of scope and lifetime , I will not repeat here what is already there, but the variable exists inside the whole function, if it has a function inside it, the variable still exists inside because it did not leave the outer function.
Being a parameter or a local variable does not change anything. Maybe you're asking two questions in one. I think here you want to know more how an anonymous function works . See also How Closures Work in JavaScript? . And about the hoisting .
Conclusion
Parameter is a variable that will receive a value during the "function" call, so something will vary during execution.
Obviously this example does not help you understand so much the use of procedures (or even functions). Exchanging a simple run for another brings little benefit. But you can even bring it. There you go into the matter of abstraction, which I think does not fit here. The question is interesting because most programmers know how to create a function / procedure, but are not sure what it actually works for, and when it's useful or not.