Dart Advanced Functions

Add Your Heading Text Here

Dart functions don’t need classes for writing different functions. Which means dart treats functions as first class objects.

Dart supports advance level functions like lambda functions.

Lambda functions are a concise and efficient mechanism to represent functions with lesser code and better performance.

Syntax of Lamda :

returnType functionName(arguments) => expressions;

For ex :

game(){

/* here return type will be decided at runtime based on the value return from the function. In this case we are returning 100 so, the return type of function game is int type.*/

return 100;

}

// above code can be written as,
game()=>100;

I think this is amazing because Lambda function removes boiler plate code from your programming and optimize your work efficiency.

Lambda function with parameters

parameters passed in lambda functions do not need to specify the data type. The data type of the parameter will be interpreted based on the value passed.

For ex :

game(gameName,rank)=>"rank $rank game of the world is $gameName";
void main(){
  print(game("Minecraft",1));
}

in above example, we didn’t declare the data type for arguments. As It is optional to pass parameters for the lambda functions in dart.

In above example, gameName variable will be assigned to String data type as the value passed to parameter is “Minecraft” which is of String type.

This way lambda functions are useful for reducing the code as well as provding simplified syntax structure.

Optional parameters

Yes, dart supports optional parameters in the functions.

Optional parameters are the parameters which are not necessary to pass in the function call.

Optional parameters can have default values (if any values are not passed for optional parameters the optional parameter will have that default value we have provide in the function definition signature).

Optional parameters can be declared only after the required parameters.

There are two types of optional parameters

  1. Positional parameters
  2. Named parameters

Positional Parameters

Positional parameters are the type of optional parameters which are included inside square brackets – [ ] .

In any function, if you have declared more than one positional parameters then you have to pass them in the same order as you have defined in the function signature.

For ex :

void main(){
		// order of passing parameters is important in function call with positional parameters.
		watchMovies("An Action Movie","Star wars", 1999);
		watchMovies("An Action Movie","Terminator");
}

// watchMovies() has default int positional parameter year with value 1984. Any function call to watchMovies function without positional parameter year will be assigned default value 1984.
watchMovies(String category, [String movie, int year = 1984]){
  print(movie + " was $category released in "+ year.toString());
}

Output :

Star wars was An Action Movie released in 1999
Terminator was An Action Movie released in 1984

Named Parameters

Named parameters are the type of optional parameters which are included inside curly brackets – { } .

Named parameters are the parameters which can be passed in function call with the name we have used for that particular parameter in the function signature.

To pass named parameters in function call, you must have to use parameter name defined in the function signature.

Function calling with named parameter will be look like below,

// Here, parameterName is the name defined in function. ParameterValue will be assigned to that parameterName.
function call(parameterName : parameterValue);

For ex :

void main(){
  // Here, year and series are named parameters which are defined in the method signature of watchSeries().
 watchSeries("A Drama Series",year: 2011, series: "Game of Thrones");
}

// Any function call to watchSeries() function without named parameter year will be assigned default value 3000.
watchSeries(String category, {series, year=3000}){
  print(series + " was $category released in "+ year.toString());
}

Output :

Game of Thrones was A Drama Series released in 2011
The Walking Dead was A Drama Series released in 2010

In named parameters, you can change the order of passing the optional parameters in function calling as we did in above example.

These are some of the advance approaches available in dart functions to make your programs smoother and easy to code.

Also, dart programs can run with only functions without use of class-object structure.

This shows that how much vital role functions play in dart language. So, mastering functions in dart will help your large projects more redable.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons