5. Dart - Function Basics

Function Parameters:

function(int variableint) {

    print(variableint)

}

// lets functions use variables given when called.

// can add more than one parameter, just separate them with commas.

// can add a type (String, int, etc) prior to function name to specify it more.

// optional paramenters are encased with square brackets.



Adding Parameters to Function Calls:

    function(5);


Returning Variables from Function:

    function(int variablenum) {

        variable = variablenum + 2

        return variable;

    }


    int test = 5;

    x = function(test);

    // returns the variable to where it was called.


Return Example 2:

String functionname() {

    return "string"

}

// declares the return type in front of the function name


Void:

    void functionname() {

        stuff here

    }

    // Indicates that the function does not and can not return anything,


Single Line Functions: (when want to return a single value)

    function(int test) => print(test);

    void function(int test) => print(test);

    function(int a, int b) => a * b; //implied return is applied here






Comments

Popular posts from this blog

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

20. Data Analytics - Analyze Data to Answer Questions - Week 1

3. Algorithms - Selection Sort