o7planning

Dart Functions Tutorial with Examples

  1. What is function?
  2. Regular function
  3. Closure
  4. Optional parameter
  5. Optional Named Parameter

1. What is function?

In the Dart programming language, a function is a named block of code, which can take parameters and return a value or return, and only run when it is called. Functions divide large tasks into small parts and perform specific operations of that program. This process increases code reusability and enhances the program's modular approach.
return_type function_name(arguments)  {
   // Statements
}
Note: Similar to JavaScript, Dart does not allow two functions with the same name, even if they have different parameters.

2. Regular function

Syntax for declaring a regular function:
return_type function_name(data_type1 arg1, data_type2 arg2, data_typeN argN)  {
   // Statements
}
Use the void keyword (or empty) as a return type if the function returns nothing.
void function_name(data_type1 arg1, data_type2 arg2, data_typeN argN)  {
   // Statements
}
// Same as:
function_name(data_type1 arg1, data_type2 arg2, data_typeN argN)  {
   // Statements
}
Example: The sum function takes two arguments of int type, and returns a value of int type.
function_ex1.dart
int sum(int a, int b)  {
  return a + b;
}
void main()  {
   var result  = sum(100, 200); // 300
   print('result = $result');  
}
Example: The greeting function accepts a String parameter and returns nothing.
function_ex2.dart
void greeting(String name)  {
   var s = 'Hello $name';
   print(s);
}
void main()  {
   // Call the function:
   greeting('Tom'); // Hello Tom
}

3. Closure

In the Dart programming language, a Closure is a special function.
  • Similar to a function, a Closure is a block of statements with parameters and can return a value or nothing.
  • Unlike a function, a closure has no name. However, you can identify it through a variable.
See detailed article about Closure:

4. Optional parameter

As mentioned above, Dart does not allow functions with the same name, but a function can include optional parameters, which must be the last parameters in the parameter list.
Syntax:
return_type function_name(typeM argM, typeN argN, [typeP? argP, typeQ? argQ]) {  
     // statement(s)  
}
Or syntax - Optional parameter with default value:
return_type function_name(typeM argM, typeN argN,
                          [typeP? argP = defaultValueP, typeQ? argQ]) {  
     // statement(s)  
}
Example:
function_optional_args_ex1.dart
String concat(String s1, String s2, [String? s3]) {
  if (s3 != null) {
    return s1 + s2 + s3;
  }
  return s1 + s2;
}
double sum(double v1, double v2, [double? v3, double? v4]) {
  return v1 + v2 + (v3 ?? 0) + (v4 ?? 0);
}
void main() {
  String result1 =  concat('One', 'Two');
  print('result1: $result1');
  String result2 =  concat('One', 'Two', 'Three');
  print('result2: $result2');
 
  double value1 =  sum(1, 2, 3, 4);
  print('value1: $value1');
  double value2 = sum(1, 2, 3);
  print('value2: $value2');
  double value3 = sum(1, 2);
  print('value3: $value3');
}
Output:
result1: OneTwo
result2: OneTwoThree
value1: 10.0
value2: 6.0
value3: 3.0

5. Optional Named Parameter

Optional Named Parameters are parameters enclosed in braces { } and placed at the end of the function's parameter list. All these parameters have default values.
Syntax:
return_type function_name(typeM argM, typeN argN,
                                   {typeP argP = defaultValueP, typeQ argQ = defaultValueQ}){  
     // statement(s)  
}
Example:
function_optional_named_args_ex1.dart
String concatAndTrim(String s1, String s2, {bool trimLeft = true, bool trimRight = true}) {
  var s = s1 + s2;
  if (trimLeft && trimRight) {
    return s.trim();
  } else if (trimLeft) {
    return s.trimLeft();
  } else if (trimRight) {
    return s.trimRight();
  }
  return s;
}
void main() {
  var s1 = '  One  ';
  var s2 = '  Two  ';

  var result1 = concatAndTrim(s1, s2); // trim left and right
  print('result1: -->$result1<-- (Trim left and right)');

  var result2 = concatAndTrim(s1, s2, trimLeft: false); // trim right only
  print('result2: -->$result2<-- (Trim right only)');

  var result3 = concatAndTrim(s1, s2, trimRight: false); // trim left only
  print('result3: -->$result3<-- (Trim left only)' );

  var result4 =
      concatAndTrim(s1, s2, trimLeft: false, trimRight: false); // no trim
  print('result4: -->$result4<!-- (No Trim)');
}
Output:
result1: -->One    Two<-- (Trim left and right)
result2: -->  One    Two<-- (Trim right only)
result3: -->One    Two  <-- (Trim left only)
result4: -->  One    Two  <!-- (No Trim)