×

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias of the function type can be used as an annotation type in a variable declaration or a return function type. Typedef stores type information when assigning a function type to a variable.

The typedef keyword is used to create an alias for function that will be the same as the actual functions. We can also create a prototype of the function with a list of parameters. The syntax is provided below.

Syntax :

typedef function_name( parameters )  

Example -

Let's create an alias of func( int f1, int f2 ) that contains two parameters of the integer type.

Typedef func( int f1, int f2 ) ;   // function signature 

Assigning typedef Variable

We may assign any function with the same parameter for typedef variable. The syntax is provided below.

Syntax:

type_def var_name = function_name( ) ; 

Let’s understand the following example, where we describe two functions with the same signature as func( ).

Sum( int a, int b ) {  
      print( " Sum of the two numbers is : ${ a + b } " ) ;  
}  
Sub( int c , int d ) {  
      print( " Subtraction of the two numbers is : ${ c - d } " ) ;  
}  

Calling Function with typedef

We can request a function by passing the same parameter using the typdef variable. The syntax is provided below.

Syntax:

var_name( parameter ) ; 

Example :

func f ;  
f = Sum ;  
f( 5, 10 ) ;  
f = Sub ;  
f( 15, 20 ) ;  

‘ f ’ is a typedef variable, which can be used to refer to any method that accepts two integer parameters. The function reference can be changed during operation using typedefs.

Complete Program using typedef

Let's have a look at the following example.

typedef func( int n1, int n2 ) ;  // typedef function signature  
Sum( int a, int b ) {  
      print( " Sum of the two numbers is : ${ a + b } " ) ;  
}  
Sub( int c, int d ) {  
      print( " Subtraction of the two numbers is : ${ c – d } " ) ;  
}  
void main( ) {  
func f = Sum ;  
print( " This program depicts an example of typedefs in Dart " ) ;  
f( 5 ,10 ) ;  
f = Sub ;  
f( 3 , 20 ) ;  
}  

Output :

This program depicts an example of typedefs in Dart 
Sum of the two numbers : 15
Subtraction of the two numbers : 23

Explanation :

In the code above, we created an alias for the ‘ func( ) ’ function using a typedef keyword. We have described two additional functions ‘ Sum( ) ’ and ‘ Sub( ) ’, which have the same signature function as typedef function.

Then, assign a variable type of typedef ‘ f ’ referring to both the ‘ Sum( ) ’ function and the ‘ Sub( ) ’ function. Now, we invoked the function by passing the required argument, and it printed the result on the screen.

Typedef as Parameter

We can use typedef method as a parameter. In the following example, we create additional function in the above program func1( int n1, int n2, func f ) with two-integer variables and typedef func f as its parameter.

Example -

typedef func( int f1, int f2 ) ;  // declaration of the typedef function func( )
Sum( int n1, int n2 ) {  
      print( " Sum of the two numbers is : ${ n1 + n2 } " ) ;  
}  
Sub( int n1, int n2 ) {  
      print( " Subtraction of the two numbers is : ${ n1 - n2 } " ) ;  
}     
func1( int n1, int n2, func f ) {  
      print( " Inside the func1( ) function " ) ;  
      f( n1, n2 ) ;  
         }  
void main( ) {  
print( " " ) ;  
func1( 20, 10, Sum ) ;  
func1( 20, 10, Sub ) ;  
} 

Output :

This program depicts an example of typedefs in Dart 
Inside the func1( ) function 
Sum of the two number : 30
Inside the func1( ) function 
Subtraction of the two number : 10

Dart Debugging

In the code above, we did not create a typedef variable to refer to each method; we just called the ‘ func1( ) ’ function by passing the required value and variation of typedef mp. It Performed the given operations and printed the result.

Debugging is a process of identifying and eliminating existing and possible errors in the Dart program that can cause ambiguity and uncertainty during the program execution. Debugging is essential to find and fix the bugs to run the program smoothly or without interruption.

Debugging is easier when you use the IDE for the Dart program. Here we assume that you have installed the most common and appropriate IDE WebStorme in your system. WebStorm Editor allows us to debug the error step by step.

What are Breakpoints?

Breakpoints are the check points of the program to break the system at a particular point to test its behaviour. We can add breakthroughs to the program to check for bugs in that particular area.

How to add Breakpoints in WebStorm?

We can add breakthroughs to WebStorm by simply clicking on the line number in the left bar to add a breakpoint. After adding the breakpoint, run the program in debug mode, it will provide a Debugger window where we can verify how the breakpoint works. And we can change values and see the difference in the watches window.


Related Topics

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

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

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

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

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

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

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 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 Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

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