×

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

Related Topics

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.