Functions as First Class Objects in Dart | Functions as Parameter | Return a functions

Functions as First Class Objects in Dart | Functions as Parameter | Return a functions

Functions as First Class Objects in Dart:

Basically in Dart, functions are first-class citizens. This means that you can treat a function as a value of other types.

So you can,

Assign a function to a variable.

Pass a function to another function as an argument.

Return a function from a function.

  • Assign a function to a variable

    Functions as a Parameter:

    Dart allows you to pass functions as parameters to other functions, enabling you to customize behavior dynamically.

    We have two functions:

    1. say: This function takes two parameters - a String message and a Function named customGreeting. Inside the function, it prints the message and then calls the customGreeting function.

    2. main: This is the entry point of the Dart program. Inside main, we call the say function with the message "Hi there!" and a function as the second argument. This function is an anonymous function (a function without a name) that prints "Custom Greeting!".

      Return a functions:

      You can return functions from other functions in Dart, allowing you to create functions dynamically.

      This demonstrates how you can return a function from another function in Dart, allowing for dynamic generation of functions based on parameters passed to the enclosing function.