Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very useful as they reduce the time and memory of re-writing the same lines of code repeatedly.

Functions make it easier to divide the larger programs into smaller sub-groups known as modules, and this type of programming refers to modular programming. They also enhance the reusability and readability of the code.

An important point to remember here is that a function can only have one return type, and the return type and function's return value must be the same.

Functions can be categorized based on arguments and return type as follows :

  1. Function with no arguments and no return type
  2. Function with arguments and no return type
  3. Function with no arguments and return type
  4. Function with arguments and with return type

Let us study each one of them in detail:

Function with no arguments and no return type

In this type of function, we do not provide any argument to the function and neither do we provide any return type to it, which is basically 'void'.

Let us consider the following example to understand this type of function,

Program

/* defining the function display( )

Cleary there is no argument to this function and the return type is void which

technically means that the function doesn't return anything */

void display( )

{

print( ' This is the example of function with no return type and no arguments ! ' ) ;

// no return statement

}

void main( )

{

print( ' Let us see : ' ) ;

/* call to the function display.

Since the function originally did not have any argument or return statement, we do

not provide either of them in the call statement */

display( ) ;

}

Output:

This is the example of function with no return type and no arguments!

Function with no argument but return type 

In this type of function, we do not provide any arguments to the function, but it has a return type which can be int or double.

It must be ensured that the return type and return value of the function must be same.

Let us consider the following example to understand this type of function,

Program

/* defining the function increment( ) which increments the value of a by 1

Cleary there is no argument to this function but the return type is int which

technically means that the function returns an integer value */

int increment( )

{

int a = 10 ;

print(' Value of a before incrementing : ' ) ;

print( a ) ;

a ++ ;

return a ; // return statement

}

void main( )

{

int a ;

/* call to the function increment

Since the function originally did not have any arguments, we did not pass any

in the call statement. But a variable of type int is declaared to hold the

integer value returned by the function. */

a = increment( ) ;

print( ' Let us see the value of a after incrementing : ' ) ;

print( a ) ;

}

Output

Value of a before incrementing :

10

Let us see the value of a after incrementing :

11

Functions with arguments but no return type

In this type of function, we provide arguments to the function but it has no return type which means we use void as the return type.

Let us consider the following example to understand this type of function,

Example -

/* defining the function increment() which increments the value of a by 1

Here we provide an argument to this function but the return type is void which

technically means that the function does not return anything */

void increment( int a )

{

print( ' Value of a before incrementing : ' ) ;

print( a ) ;

a ++ ;

print( ' Value of a after incrementing : ' ) ;

print( a ) ;

}

void main( )

{

int a = 5 ;

/* call to the function increment

Since the function declartion had one argument int a, we pass an integer variable to this function which passes the value 5 to that function for computation. */

increment( a ) ;

}

Output

Vale of a before incrementing :

5

Value of a after incrementing :

6

Functions with arguments and return type

In this type of function, we provide both argument and the return type of the function which can be int or double.

Let us consider the following example to understand this type of function,

Example -

/* defining the function increment() which increments the value of a by 1

Here we provide both an argument and the return type to the function.

The return type is integer which means the function returns an integer value. */

int increment( int a )

{

  print( ' Value of a before incrementing : ' ) ;

  print( a ) ;

  a ++ ;

  print( ' Value of a after incrementing : ' ) ;

  return a ; // return statement

}

void main( )

{

  int a = 5 ;

    /* call to the function increment

  Since the function declaration had one argument int a, we pass an integer

Variable to this function which passes the value 5 to that function for computation.

A variable is used to hold the value returned by the increment( ) function */

  a = increment( a ) ;

  print( a ) ;

}

Output:

Value of a before incrementing :
5
Value of a after incrementing :
6

There are three other types of functions as well, which are as follows :

  1. Dart Recursive Functions
  2. Dart Lambda Functions / Dart Arrow Functions
  3. Dart Anonymous Function