The Methods and Parameters

Dart Learning [Part 2]

Concise and fast way through Dart methods and parameters

Shrijit_Basak
My Coding Experiments

--

Welcome back to the “crisp and concise” tutorial to learning Dart. This post will cover the Dart intermediate stuff like methods and parameters. If you have not already learned the dart basics please give this previous post a quick go through before continuing with this post.

By now you must be pretty aware that I am a sucker for TLDR; section for any tutorial so let’s quickly go through of what we will learn in detail in the following sections.

Tutorial Level: ★★☆☆☆(Intermediate)

Skill level after this post…

You can use the dartpad for running dart codes online.

TLDR;

If you want to read the detailed version then you can skip to this section.

If you are already a bit familiar with the dart jargons or already know Java, Python or JavaScript then you can just have a quick look at the syntactical structure and move on to the 3rd post.

TLDR;

[You can execute the above code here.]

Hooray!!…Congratulations!! 🥳🎉.
Now you can move on to the next post and learn the advanced level stuff about Dart.

Your Dart skill has increased…

The full version…

Methods: As we all know the first thing that is executed by Dart is the void main() . Here void main is the default method which the Dart virtual machine begins executing in. Like void main, we can create other methods also.

Methods are basically blocks of codes written separately from the regular code and later invoked by calling the method’s name. There can be many reasons why one would write methods like,
* improving readability,
* refactoring the code to group similar blocks of code together,
* reusing the block in multiple places by defining it in one place and calling the method again and again
*and many more…

A method definition has a certain structure to follow:
1. Method return type: This defines the datatype of the value that will be returned by the method once it is done executing. It can be either void that means no value will be returned or it can be one of the datatypes we have defined earlier in this post.
2. Method name: After the return type comes the method name. In void main() the name of the method is “main”. Like variables, this name can also start with an underscore to denote its a private method.
3. Parameters: After the name, we put the parameter list wrapped in “(“ and “)” bracket(aka parenthesis). The list of parameters define the values the method will be needing to function properly.
Dart provides some other features for the parameters. The parameters can be either normal, or optional or named or optional with a default value or named with a default value [The type of parameters are covered in the next section].
4. Body: After the list of parameters comes the body of the method. The body is usually wrapped around the “{“ and “}” braces. Dart also allows us to write a single line body method following the “=>” operator after the parameter list without enclosing the body in a “{“ and “}” braces.

Parameters: As I mentioned earlier, Dart allows 5 ways for defining a parameter.
Note: Every parameter needs to have a datatype and name associated with it, just like a variable. And every parameter needs to be mentioned one after the another using the comma “,” in place of “;” compared to as we do for variables.

1.Normal Parameter: A normal parameter just has a name and a datatype. This is the most common way of defining a parameter.

Normal Parameters Example

2.Optional Parameter: An optional parameter as the name suggests are optional to pass when calling the method. These parameters are further enclosed inside “[“ and “]”(aka square braces) to show that they are optional. These parameters are initialized with null if the caller doesn't pass a value for these parameters.

Optional Parameters Example

3. Optional Parameter with default value: Optional parameters can also be given a default value so that if the caller doesn’t pass a value to these parameters, it can take up the default value in those situations.
Note: the default value has to be a compile-time constant and can’t be a variable.

Optional Parameters with default value Example

4. Named Parameter: Named parameters are also optional in nature but they have a special feature that is that they can be “named” while the method is called. That means that while calling the method we can pass a certain value to a certain parameter by naming that parameter. These parameters are enclosed in “{“ and “}”(aka curly braces).

Named Parameter Example

5. Named Parameter with default value: Named parameter can also be given a default value so that if the caller doesn't pass a value to these parameters, they can take up the default value in those situations.
Note: the default value has to be a compile-time constant and can’t be a variable.

Named Parameter with default value Example

Hooray!!…Congratulations!! 🥳🎉.

<GTA SanAndreas theme playing in the background!!>

You have now learnt about the methods and parameters in Dart and your Dart skill has now increased.

Your Skill Meter…

Let’s fill up this meter by continuing this learning in this next post.

I hope you had fun learning about the methods and parameters in Dart today, this post is just the second part to the whole series up ahead. Please do give this post a Clap and follow me for more Dart tutorials and other contents.

--

--

Shrijit_Basak
My Coding Experiments

Computer Science enthusiast, gamer, Linux fan(i use arch btw….), foodie.