Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn Dart Function

In Dart, functions are a fundamental concept that allow you to group code together into reusable blocks. Named parameters are a feature of Dart functions that allow you to specify parameters by name when calling the function, providing more clarity and flexibility.

Defining function : You can define functions using the void keyword if they don't return any value, or specify the return type if they do return a value. Here's a basic function without named parameters:

void greet(String name) {
print('Hello, $name!');
}


Using Named Parameters: Named parameters are defined by enclosing them in curly braces {} in the function definition. This allows you to specify parameters by name when calling the function.

void greet({String name, String greeting}) {
print('$greeting, $name!');
}


Calling Functions with Named Parameters: : When calling a function with named parameters, you specify the parameter names followed by a colon : and the corresponding values.

greet(name: 'Alice', greeting: 'Hi'); // Output: Hi, Alice!


Default Values for Named Parameters:: You can also specify default values for named parameters in Dart. This allows you to call the function without providing a value for that parameter if you're happy with the default.

void greet({String name = 'World', String greeting = 'Hello'}) {
print('$greeting, $name!');
}
greet(); // Output: Hello, World!
greet(name: 'Alice'); // Output: Hello, Alice!


Named parameters are especially useful when a function has several optional parameters or when you want to make the code more readable by explicitly specifying parameter names when calling the function. They provide flexibility and clarity, especially in functions with multiple parameters.






Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java