When does
function funcParm() {
$foo = "parâmetro";
echo "$foo";
}
in practice is making a procedure and not a function, despite being declared so.
Why have a procedure?
Avoid code repeats, or at least organize a set of instructions contained in a single place giving a name to it. How this name can refer to it at some point in the code by name and what is inside will be executed.
The utility is reduced if it will use only once, but it can still be useful to give a separate responsibility, better document that those instructions are part of one thing only. In such abstract examples it is not always clear that.
If you are going to use more than once the same code there is the obvious advantage of not having to repeat what has already been written and you may be doing something more DRY, which is a desirable feature of fancy code.
The big difference for a function is that the latter should return a value. It’s okay to call it a function, everyone understands, but formally it’s not quite a function.
Note that in the example the variable is totally unnecessary. 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 to parametrize?
There are cases that each time you call a procedure, you want that set of instructions to be executed, but a small portion of the code is a little different. That is, there is a gap in it that must be filled in each run. It is a part variable of the code, precisely because it varies in each call. Even more, when you call the procedure will tell which is this variable part. So what we do is parameterize the function.
Parameter is just a variable that will define something.
pa râ me tro (for- + meter) male noun
[Geometry] Constant line that enters the equation or construction of a curve, and serves as a fixed measure to compare the ordinates and the
abscissas..
Feature or variable that lets you define or compare something.
"parameter", in Dicionário Priberam da Língua Portuguesa [online],
2008-2013, https://www.priberam.pt/dlpo/par%C3%A2metro [consulted in
05-01-2017].
Then when calling the procedure an argument is passed, which is a value that will be assigned to the variable that serves as parameter. To help those who are coming here and don’t know What is the difference between parameter and argument?.
This gives flexibility in code usage. This example above will always print the same thing. Now:
function funcParm2($foo) {
echo "$foo";
}
funcParm("parâmetro");
is printing this text in this example. Call yourself 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 necessary.
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();
I put in the Github for future reference.
Here is a matter of life span and time, I will not repeat here what is already there, but the variable exists within the whole function, if it has a function within it, the variable continues to exist inside, because it has not left the most external function.
Being a parameter or a local variable doesn’t change anything. Maybe you’re asking two questions in a . I think here you want to know more how an anonymous function works. See also How Closures work in Javascript?. What about the Hoisting.
Completion
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 to understand so much the use of procedures (or even functions). Exchanging one simple execution for another brings little advantage, but can even bring. Then it comes to the subject of abstraction that I think does not fit here. The question is interesting because most programmers know how to create a function/procedure, but they don’t really know what it’s for, and when it’s useful or not.
Is there anything else you think needs to improve the answers?
– Maniero