×

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

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.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 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 Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 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.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 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 Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.