Dart function introduction with examples

Published by admin on

Introduction to dart function :

Functions are blocks of reusable code. If you have a piece of code that is used in multiple places of your program, you can put it in a function and use it instead of the same code block. A function can take values and it can return one result.

If you use a function, you don’t have to write the same piece of code repeatedly and the program looks neat. Also, don’t make it too lengthy. Functions are not only for the developer who is writing, but these are also for other developers who may work on the same project in the future. So, make it clean and easy to understand.

In this post, we will learn how to write functions in dart, types of functions and different properties of a function.

 

Write your first dart function :

The function is defined as below :

[return_value] function_name([..parameters]){

}

Here,
return_value is the type of value the function is returning. This is optional in dart.
function_name is the name of the function. It is required. This name is used to call the function from any other place.
parameters are parameters i.e. the values we are passing to the function. It can be a single parameter or multiple parameters. This is optional i.e. we can have functions without any parameter in dart.

Examples of different types of dart functions :

main function :

Like most other programming languages, dart also has one main function that acts as the entry point. This is the method that is called first. Normally, inside the main function, initialization is done. This function has an optional List parameter that can be used to pass arguments from the command prompt.

void main(List args) {
}

This is a complete main function. args is optional and we can also write this function without the return type void. So, if you are not taking any arguments, you can simply write it as :

main(){

}

 

Types of parameters in dart function :

Parameters are passed inside a function. Parameters are categorized into two types in dart : required and optional. Again, optional parameters are divided into named or positional. The required parameters are placed first and followed by optional parameters.

Required parameter :

The required parameters are always needed to pass. We can’t exclude any required parameters. For example :

printMessage(String param1){
  print('param1 : $param1');
}

main() {
  printMessage("Hello");
}

Here, param1 is a required parameter. We can’t call this function printMessage without param1.

 

Optional parameter :

Optional parameters are optional i.e. the function will work even if you don’t pass these parameters. They can be named or positional parameters, but not both.

 

named parameter :

Named parameters are enclosed in curly braces {}. You can call them using name : value :

printMessage(String param1, {String param2, String param3}){
  print('param1 : $param1');
  print('param2 : $param2');
  print('param3 : $param3');
}

main() {
  printMessage("Hello", param2: "World", param3: "!!");
  printMessage("Hello");
}

Here, param2 and param3 are optional parameters. If we don’t pass them, the value will be null.

Dart optional named parameters

You can use @required with named parameters to make them required.

 

positional parameter :

Positional parameters are optional parameter enclosed in []. You don’t need the name to indicate them like named parameters :

printMessage(String param1, [String param2, String param3]) {
  print('param1 : $param1');
  print('param2 : $param2');
  print('param3 : $param3');
}

main() {
  printMessage("Hello", "World", "!!");
  printMessage("Hello");
}

Dart optional positional parameters

It prints the same output as the above example. The only difference is that we are not using the name reference.

 

Default values :

We can have default values with optional parameters. These values will be used if we don’t pass any value to these parameters. = is used to assign a default value to a parameter. For example :

printMessage({String param2 = "param2", String param3 = "param3"}) {
  print('param2 : $param2');
  print('param3 : $param3');
}

main() {
  printMessage();
}

We are assigning default values to param2 and param3. This will print the below output :

param2 : param2
param3 : param3

As we are not passing any values to the parameters, it will print the default values.

 

Arrow function :

Arrow functions are a short way to write a function in dart. We can put one expression in an arrow function :

printMessage(String message) => print(message);

main() {
  printMessage("Hello world !!");
}

Arrow functions are useful if you have a function with one single expression.

 

Assign function to a variable and pass it as parameter :

In dart, functions are objects. So, we can assign one function to a variable and we can pass them to other functions. The functions are of type Function in dart.

 

Anonymous function :

Anonymous function is also called lambda function or closure. This is similar to other normal function, but it is nameless. The parameters are passed inside () and the body is started inside a curly braces {}. One common example is list iteration. We can pass one anonymous function to the forEach method that takes one parameter :

main() {
  var nos = [1,2,3,4,5];
  nos.forEach((n){
    print(n);
  });
}

These are the basics of dart function. Go through the examples explained above and drop one comment below if you have any queries.

Categories: dart

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *