Parameters in Dart
Tags: dart, codenewbie
Published: 2019-09-18 18:10:02


Hi there! in the last post we've been talking about **variables** and **functions.** If you missed it, you can check our previous post here.

### **[Variables](https://dev.to/codingpizza/variables-in-dart-4j63)**

### **[Functions](https://dev.to/codingpizza/functions-in-dart-175b)**

## We saw them previously.

On the previous post about **functions** we talked about *parameters.* We saw an example in which we need them as ingredients to make an IceCreamMachine works, we can also say that parameters are dependencies that a function requires to execute their code. 

## Required parameters

The required parameters are the most basic parameters that a function can use, you specify a type, a name and you're ready to go.

We already saw them in the previous post on the sum function example:

    Integer sum(Integer a,Integer b) {
    	return a+b;
    }

The integer **a** and the integer **b,** are used inside the function and returned therefore.

## Optional parameters

These parameters are optional because you can ommit them when you use that function. To made a parameter optional we need to put them inside brackets **and** at the end of the signature, if you're using **required params.** Let's see and example.

    void printFullName(String name, String surname, [String secondName]){
    	print("My name is: $name $secondName $surname");
    }

In this function we can see how the **optional parameter** is placed before the **required parameters,** if you put the **optiomal parameter** first the compiler would complain. 

Ok, but what happened with the variable `$secondName` if is not passed? The variable is going to be null. We don't want to print "John null Wick", right? for that we can add a default value that is going to be used in case the **optional parameter** is null. 

To add a default value to a **optional parameter,** all we need to do is an assignment. You can see it better in the following example:

    void printFullName(String name, String surname, [String secondName = ""]){
    	print("My name is: $name $secondName $surname");
    }

Now the value is going to be an empty string and the name will print correctly.

Let's talk about how we can use the previous function. We actually can use it as follows:

    printFullName("John","Wick");
    //Or
    printFullName("Scarlett","Johansson","Ingrid");

You can made all your parameters optional by wrapping your parameters with brackets, as follows:

    void printFullName([String name, String surname, String secondName]){
    	print("My name is: $name $secondName $surname");
    }

---

## Named parameters

This parameters allows you to indicate in the function signature which parameter are you passing into. For do that we need to surround our parameter with curly braces.

Here's an example:

    void printNamedParameters(String name,String surname,{String secondName = ""}){
    	print("$name $secondName $surname")
    }

In this example we're using the name and the surname as required parameters. And the secondName as **named parameter** and in case nothing is passed to it the value will be an empty string.

When we want to use the last function with the optional parameters we use it in the following way:

    printNamedParameters("Samuel","Jackson",secondName: "Leroy");

As you can see the named parameter should be included inside the parentheses.

In case we need to have a function with only **named parameters** all we need to do is to surround all the parameter section in the function signature with curly braces. 

    void printAllNamedParameters({String name,String surname,String secondName = ""}){
    	print("$name $secondName $surname")
    }

With the last function we can specify the parameters but also change the order in which we use it, because the order of the parameters doesn't matter.

For example:

    printAllNamedParameters(name: "Scarlett",secondName:"Ingrid",surname:"Johansson");

Isn't it amazing?, this improve the function readability a lot. 

## That's it

If you have reached this point, I hope you like this post. If you like it share it with your friends and comment on which topic would you like to see in this section.

## Now is your turn

You can try these concepts in IDE like Intellij idea community which is free, all you need is to install the Dart Plugin. Visual Studio Code or in some online editors like [**Dartpad**.](https://dartpad.dartlang.org/)

## Previous post

If you're interested in more post like this you can check out my others articles about Dart.

**[Variables](https://dev.to/codingpizza/variables-in-dart-4j63)**

**[Functions](https://dev.to/codingpizza/functions-in-dart-175b)**

## Learn more

Im actually writing more like these in a free ebook which is basically a basic course of Dart that can help you to later start with Flutter, that awesome framework for develop multiplatform apps. If you're interested you can get it for **free** following this link.