baha zou
baha zou

Reputation: 31

What is the logic behind these functions

I am learning Dart, and I can't understand the logic behind this code if anyone can help:

Function applyMultiplier(num multiplier) {
  return (num value) {
    return value * multiplier;
  };
}

final triple = applyMultiplier(3);
print(triple(6)); //output 18
  1. There is an anonymous function inside a named function.
  2. We assigned a function to a variable.

What I don't understand is how did we pass from triple to value. I can't understand the logic behind.

Upvotes: 0

Views: 30

Answers (1)

TheUltimateOptimist
TheUltimateOptimist

Reputation: 1219

Well, the function applyMultiplier takes a num as argument and returns a function that itself returns the value it is given multiplied by another multiplier. final tripple = applyMultiplier(3) stores this function that is returned from applyMultiplier in the variable triple. Because the variable triple then stores a function it can also be used like a function.

Upvotes: 1

Related Questions