The function is also called a method that takes inputs executes a sequence of a code block of statements and returns the output.

Functions are used and created when multiple code statements want to repeatedly execute in different places.

It helps code reusability and clean separation of code in logic and features.

In this tutorial, Learn

  • function definition
  • Function return type
  • Function

How to create and define Function in dart

The function contains the following syntax

Datatype functionname(arguments){
    //function body contains code block;
    return value;
}

functionname: Name of the function. Datatype: Function returns the value of Dart primitive or custom type arguments: optional arguments code-block: is a dart code that executes in sequential order return value: value returned from the function. the function body is enclosed in {}

Once the above function is defined, The function is called with the below syntax. Function Invocation syntax:

functionname(arguments);

functionname: function name executed arguments: parameters to the function.

Here is a simple function definition.

void main() {
  print(multiplication(12, 2));
  print(multiplication(4, 20));
}

int multiplication(int one, int two) {
  var result = one * two;
  return result;
}

Output:

24
80

This example shows how to declare a function with a void. void to a function means it does not return anything.

Function types

Functions are categorized into multiple types based on return type and arguments.

  • Functions without arguments and return type:

This example contains a function with no arguments and a return type

void main() {
  printMsg();
}

void printMsg() {
  print("Welcome John");
}
  • Functions without arguments and return type:

This example shows a function without arguments and returns a type

void main() {
  print(getMessage());
}
String getMessage() {
  return "welcome";
}
  • Functions with arguments and without return type: This example shows a method with arguments and without a return type.
void main() {
  printMsg("john");
}

void printMsg(String name) {
  print("Welcome ${name}");
}
  • Functions with arguments and return type:
void main() {
  print(substraction(12, 2));
  print(substraction(40, 20));
}


int substraction(int one, int two) {
  var result = one - two;
  return result;
}

Functions Advantages

The function has advantages in dart

  • Code declared once and used and executed in multiple places, allows reusability
  • Code separation into feature and logic wise and enhances code
  • Unit testing is very easy as you need to test functions only
  • Development and productivity is improved
  • Helps functions to debug and fix issues

Dart Functional optional arguments

Functional arguments can be options, which means the caller is not required to send arguments. There are two types of optional argument types

  • optional Named parameters
  • Positional Optional Arguments
  • Optional Arguments with the default value

optional Named parameters

In Named arguments, Each argument is named with a variable.

Named arguments are enclosed in {}.

void functionname(argument, {type arguments}) {
  // statements
}

arguments in {} contain a type and are separated by a comma. Caller function syntax

the function called with passing normal arguments with value and named arguments with argument: value syntax if the caller is not passed with optional arguments, the value null is passed.

functionname(argument,arguments:values)

Here is an example

void main() {
printNumbers(10, number2: 20);
}

void printNumbers(number1, {int number2, int number3}) {
  print(number1);
  print(number2);
  print(number3);
}

Output:

10
20
null

Positional Optional Arguments

In this, optional arguments are passed inside square brackets[]. positional arguments are assigned based on position and order defined inside [].

Syntax:

void functionname(argument, [type arguments]) {
  // statements
}

Caller function syntax if the caller is not passed with optional arguments, the value null is passed.

functionname(argument,arguments)
void main() {
printNumbers(10,  20);
}

void printNumbers(number1, [int number2, int number3]) {
  print(number1);
  print(number2);
  print(number3);
}

Output:

10
20
null

Optional Arguments with a default value

Optional arguments are passed with default values in the function declaration.

Syntax:

void functionname(argument, {type arguments=defaultvalue}) {
  // statements
}

Caller function syntax

functionname(argument,arguments:values)

In this example, the third argument is not passed assigned with default value=40

void main() {
printNumbers(10,  20);
}

void printNumbers(number1, [int? number2, int? number3=40]) {
  print(number1);
  print(number2);
  print(number3);
}

Output:

10
20
40