What are functions

Functions are a set of statements that are grouped together to perform a specific task. Functions are also known as methods, sub-routines, or procedures. Functions are used to achieve code reusability.

Dart supports the following types of functions:

  1. Named Functions
  2. Anonymous Functions
  3. Lambda Functions

Familiar with the topic? Play Quiz

You can declare functions anywhere in a file but must be called inside of main() function.

Moving forward let’s understand return type of a function in detail which will set foundation to understand funcations and its types.

(Syntax)

  
return_type function_name (parameter_list) {
   //statements
}
  

Return type in a functions

The return type of a function is the data type of the value that is returned from the function. Dart supports return type of every data type. We will take a look at the return type of the following data types:

  • int
  • void

Functions with int as return type

Since we are aware of syntax of function, we will directly jump to the example.

Example

  
void main() {
   print("The sum of 2 and 3 is ${sum()}");
}

int sum() {
   int num1 =2 ; 
   int num2 =3 ;
   return num1 + num2;
}
  

Here we have declared a function that has int as a return type. We have declared two variables num1 and num2 and assigned them values 2 and 3 respectively. We have returned the sum of these two variables.

Output

  The sum of 2 and 3 is 5
  

code Try Yourself

Note:

Functions with void as return type

Since we are aware of syntax of function, we will directly jump to the example.

Example

  
void main() {
   print("The sum of 2 and 3 is ${sum()}");
}

void sum() {
   int num1 =2 ; 
   int num2 =3 ;
   print(num1 + num2);
}
  

Here we have declared a function that has void as a return type.

We have declared two variables num1 and num2 and assigned them values 2 and 3 respectively. We have printed the sum of these two variables.

Output

  compile: lib/main.dart:2:35:
Error: This expression has type 'void' and can't be used.
   print("The sum of 2 and 3 is ${sum()}");
                                  ^
Error: Compilation failed.
  

Note:

See Error Giving Code understand the error and See the Solution

Named Functions

A dart function with a name is named functions. These are normal functions that we mostly use.

A named function is a set of statements that performs a specific task. A named function can be declared anywhere in the program. A named function can be called multiple times in a program.

Syntax

  
return_type function_name (parameter_list) {
   //statements
}
  

Example

Let’s declare a function that can add two numbers, we will give sum as the name of the function.

  void main() {
  /// calling the function inside main()
  sum();
}

sum() {
  int num1 = 12;
  int num2 = 3;
  print("Sum of the number is ${num1 + num2}");
}
  

Output

  Sum of the number is 15
  

code Try Yourself

Anonymous Functions

An anonymous function is a function that is not declared with the function name. An anonymous function can be assigned to a variable.

An anonymous function can be passed as an argument to another function. An anonymous function can be returned from another function.

Syntax

  
(parameter_list) {
   //statements
}
  

Example

  
void main() {
   var sum = (int num1, int num2) {
      return num1 + num2;
   };
   print("The sum of 2 and 3 is ${sum(2, 3)}");
}
  

Output

  The sum of 2 and 3 is 5
  

code Try Yourself

Lambda Functions

A lambda function is a function that is not declared with the function name. A lambda function can be assigned to a variable.

A lambda function can be passed as an argument to another function. A lambda function can be returned from another function. A lambda function can be used as an anonymous function.

Syntax

  
(parameter_list) => expression
  

Example

  
void main() {
   var sum = (int num1, int num2) => num1 + num2;
   print("The sum of 2 and 3 is ${sum(2, 3)}");
}
  

Output

  The sum of 2 and 3 is 5
  

code Try Yourself

Note:

Arugments and Parameters in Functions

Arguments are the values that are passed to the function when the function is called.

Parameters are the variables that are used to receive the values when the function is called. Dart supports the following types of parameters:

  1. Positioned Parameters
  2. Optional Parameters
  3. Named Parameters

Positioned Parameters

Dart supports positioned parameters. Positioned parameters are enclosed in parentheses (). Positioned parameters cannot have default values. Positioned parameters cannot be null.

Values will be assigned according to the position of the parameters declared. for eg: If lets assume a function has (a,b), and we pass (3,4). a will get 3 and b will get 4.

Syntax

  
return_type function_name (parameter_list) {
   //statements
}
  

Example

  void main() {
   print("The sum of 2, 3 and 4 is ${sum(2, 3, 4)}");
}

int sum(int num1, int num2, int num3) {
  
  /// Let's test the values passed with the position
  print("value of num1: $num1, num2: $num2, num3: $num3"); 
   return num1 + num2 + num3;
}
  

Output

  value of num1: 2, num2: 3, num3: 4
The sum of 2, 3 and 4 is 9
  

code Try Yourself

Note:

Default Parameters

Default parameters can be named or positional. Default parameters are enclosed in square brackets []. Default parameters can have default values. Default parameters can be null.

Example

  void main() {
  print("The sum of 2 and 3 is ${sum(2, 3)}");
  print("-----------------");
  print("The sum of 2, 3 and 4 is ${sum(2, 3, 4)}");
}

int sum(int num1, int num2, [int num3 = 0]) {
  print("Value num1 : $num1 num2: $num2 num3: $num3");
  int sum = num1 + num2 + num3;

  return sum;
}
  

Output

  Value num1 : 2 num2: 3 num3: 0
The sum of 2 and 3 is 5
-----------------
Value num1 : 2 num2: 3 num3: 4
The sum of 2, 3 and 4 is 9
  

code Try Yourself

Note:

Named Parameters

Named parameters are enclosed in curly braces {}. Named parameters cannot be null, either it should be required or it should have default value.

You can use required keyword to make a parameter required. Let’s understand by example.

Example

  
void main() {
  
 // Calling the function with both parameters:
greet(name: 'Alice', greeting: 'Hi');  // Output: Hi, Alice!

// Calling with only the required parameter, using the default greeting:
greet(name: 'Bob');                    // Output: Hello, Bob!

// Attempting to call without the required parameter results in an error:
greet(greeting: 'Yo');   // Error: The named parameter 'name' is required.
}


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

Output Running after commenting the line giving error.

  Hi, Alice!
Hello, Bob!
  

code Try Yourself

Note:

Quiz

You can test your understanding with the questions we prepared, it will take less than 3 minutes.