Junior — Middle
What is the difference between an anonymous function and a named function in programming?
sobes.tech AI
Answer from AI
A function without a name (anonymous function or lambda) is a function that has no name and is usually used for short-term operations, such as an argument to another function. A named function has a name and can be called from different parts of the code.
Example in Dart (Flutter):
// Named function
int add(int a, int b) {
return a + b;
}
// Anonymous function
var multiply = (int a, int b) => a * b;
void main() {
print(add(2, 3)); // 5
print(multiply(2, 3)); // 6
}
Anonymous functions are convenient for passing logic into other functions, such as event handlers or collection methods.