Page 32 - Dart Language
P. 32

Chapter 12: Functions




        Remarks



        Dart is a true object-oriented language, so even functions are objects and have a type, Function.
        This means that functions can be assigned to variables or passed as arguments to other functions.
        You can also call an instance of a Dart class as if it were a function.


        Examples



        Functions with named parameters


        When defining a function, use {param1, param2, …} to specify named parameters:


         void enableFlags({bool bold, bool hidden}) {
           // ...
         }


        When calling a function, you can specify named parameters using paramName: value


         enableFlags(bold: true, hidden: false);


        Function scoping


        Dart functions may also be declared anonymously or nested. For example, to create a nested
        function, just open a new function block within an existing function block


         void outerFunction() {

             bool innerFunction() {
                 /// Does stuff
             }
         }


        The function innerFunction may now be used inside, and only inside, outerFunction. No other other
        functions has access to it.

        Functions in Dart may also be declared anonymously, which is commonly used as function
        arguments. A common example is the sort method of List object. This method takes an optional
        argument with the following signature:


         int compare(E a, E b)


        The documentation states that the function must return 0 if the a and b are equal. It returns -1 if a <
        b and 1 if a > b.





        https://riptutorial.com/                                                                               27
   27   28   29   30   31   32   33   34   35   36   37