Table of contents
1.
Introduction
2.
TypeDef in Dart
2.1.
Defining Typedef
2.2.
Assigning Function To Typedef Variable
2.3.
Invoking A Function
3.
Examples of Typedef
4.
Frequently Asked Questions
4.1.
Is it possible to Typedef a typedef?
4.2.
Is it possible to Typedef a function in C?
4.3.
Can we make a typedef extern?
5.
Conclusion
Last Updated: Mar 27, 2024

Dart TypeDef

Author Akshit Pant
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Dart, Typedef is used to generate a user-defined identity (alias) for a function, which we can use in the program code instead of the function.

Let us assume that you have a basic understanding of programming languages and are familiar with basic Dart Introduction. In this blog, we will be covering Dart TypeDef and its implementation as a variable and as a parameter in our code.

So let us start with the basic idea of what Dart TypeDef is.

TypeDef in Dart

The Typedef is used in Dart to construct an alias for a function type, which can then be used as a type annotation for declaring variables and return types of that function type. Variable declaration or function return type can employ an alias of function type as a type annotation. When we assigned the function type to a variable, we used a typedef to store the type information.

Before we begin our deep dive into Dart TypeDef, know that you can develop your Dart sample code on DartPad.

Defining Typedef

We can use a typedef to provide a function signature that specific functions should match. The parameters of a function define its signature (including its types). The function signature does not include the return type. The following is the syntax.

Syntax:

typedef function_name(parameters);

Assigning Function To Typedef Variable

Any function with the same signature as Typedef can be referenced by a typedef variable. To assign a function to a typedef variable, use the signature below.

Syntax:

type_def  var_name = function_name;

Invoking A Function

Functions can be called using the typedef variable. This is how you can call/invoke a function.

Syntax:

var_name(parameters);

We will be able to use a single function in a variety of ways as a result of this.

Now, Let's look at an example to better grasp Typedef in Dart.

Examples of Typedef

Below given is a Dart program to demonstrate typedef usage: 

Let's start by defining a typedef. We're defining a function signature right now. The function will accept two integer-type input parameters(int a, int b), and the function signature does not include the return type.

Code:

typedef codeStudio(int a, int b); // Defining alias name
 
//  m1 function
m1(int a, int b) {
  print("You entered block m1");
  print("Adding $a + $b will be equal to ${a + b}.");
  print("");
}
 
// m2 function
m2(int a, int b) {
  print("You entered block m2");
  print("Multiplying $a * $b will be equal to ${a * b}.");
}
 
void main()   // Main method
{
  // Using alias name to define num
  codeStudio num= m1;    // num with m1 function

  num(10,100);    // Calling num
   
  // Redefining num
  num= m2;     // with m2 function

  num(2,3);    // Calling num
}


Output:

You entered block m1
Adding 10 + 100 will be equal to 110.

You entered block m2
Multiplying 2 * 3 will be equal to 6.


Apart from that, Typedef can also be used as function parameters.

So now, let's have a look at one more example of using Typedef as a function parameter.

Code:

typedef codeStudio(int x , int y);   //function signature
Adding(int x,int y){     // m1
   print("sum is ${x+y}");
}  
Subtracting(int x,int y){    //m2
   print("resultant is ${x-y}");
}  
Dividing(int x,int y){    //m3
   print("quotient is ${x/y}");
}  
Counter(int a,int b ,codeStudio operation){   // calling out operations
   print("Entering calci mode: ");
   operation(a,b);
}  
main(){ // main method
   Counter(10,20,Adding);
   Counter(30,40,Subtracting);
   Counter(50,60,Dividing);
}


Output:

Entering calci mode:
sum is 30
Entering calci mode:
resultant is -10
Entering calci mode:
quotient is 0.8333333333333334

Frequently Asked Questions

Is it possible to Typedef a typedef?

Except for type-specifiers, the typedef specifier cannot be coupled with any other specifier. The typedef-names are not declarations of new types but rather aliases for existing types. So, Typedef cannot be used to change the meaning of a type name that already exists (including a typedef-name).

Is it possible to Typedef a function in C?

The typedef keyword is used in the C programming to give out meaningful names to the variables that already exist in the program. It also works similarly to how we construct aliases for commands.

Can we make a typedef extern?

Every declarator in a declaration that uses Typedef as a storage-class specifier defines an identifier as an alias to the type provided. A typedef declaration cannot be static or extern because only one storage-class specifier is allowed in a declaration.

Conclusion

In this article, we learned about Dart TypeDef and from its declaration to usage with the implementation of examples.
However, learning never stops, and there is more to learn. So head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.
We hope this article has helped you enhance your knowledge as a Dart beginner. If you want to learn more, check out our article on environment setup and Competitive Programming articles. Do upvote this article to help other ninjas grow.

Happy Coding!

Live masterclass