The same as any language :P
First we are talking about creating a parameter and typical functions are all parameterized to have more relevance. Usually a function should perform something with a missing information that it does not know beforehand, it will receive this information and do something with it. When a function receives zero parameters it can do something very limited and in general it is only being used for flow control, that is, it is only a way to avoid a goto
, and even call it a function.
Note that in object-oriented languages there is an implicit parameter in methods called this
(Dart) or self
, so even if you don’t see it in the method signature it’s there.
The other thing we’re talking about in the question is the use of anonymous function, that is, an object that "holds a function" (actually it holds a reference to a function). So since almost every Dart language has functions of first class and the type of this object clearly is Function
.
This type of object is used as a mechanism for callback.
In the case you are mixing the two things, then you have an object that "is a function" and a parameter. This way you can parameterize a code execution. The example presented does not show this very well, but so gets better:
void metodoComFuncao(List<int> lista, Function func) {
for (var i = 0; i < lista.lenght; i++) if (lista[i] % 2 == 0) func();
}
I put in the Github for future reference.
There it calls:
metodoComFuncao(lista, funcao)
Note that you do not use parentheses in funcao
, if used it will call the function there at the time and its result is that it would be passed as argument, which in case would be wrong because the result is a void
and an object of the type Function
.
This way you’re running something within this function that doesn’t matter to you what it is, it’s implementation detail, but you know that at some point you want it to do something that you set somewhere, in case the function funcao()
. If you want something else to perform you will pass another function that will perform something else.
It is also possible to use a syntax of lambda nor create a separate function, you already create and pass the function there in the argument.
So you create better abstractions, isolating something that is common to do but what to do with complete accuracy you still don’t know, missing a part, you have to "fill in the underlined part", which is precisely what the parameter gives you, only in this case instead of putting in this missing part a number or a text as is most common you place a function that will be executed and possibly give a result (in this example it only performs even, which is more rare).
Some people find it so sensational that they begin to smear themselves :)