Dart Tutorial : Functions in Dart(Examples)

Functions are the building blocks of readable, maintainable, and reusable code. A function is a set of statements to perform a specific task. Functions organize the program into logical blocks of code. Once defined, functions may be called to access code. This makes the code reusable. Moreover, functions make it easy to read and maintain the program’s code.


A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.


Let’s see how functions work. This section is a basic introduction, and I will cover this topic more in-depth later when discussing methods in object-oriented programming.




Important Point:

Before writing a function, you need to remember these major points:


Define the type of a function. So, type annotation is recommended


Dart recommends type annotation, a function still works without any type declaration


Map<String, dynamic> arguments = {'John': 'Smith', 'Chicago': 42};

Alternatively, you can use var and let Dart infer the type:


var arguments = {'John': 'Smith', 'Chicago': 42}; // Map<String, Object>

the value you want to return from a function, you need to change the type of that function accordingly. If you want an integer value to return, for example, you should change the type of the function to an integer.


Simply put, for the void, nothing is returned from a function.


The Void() Function

So, whenever you use the keyword void before the function, you need to use print(object) to see what’s happening inside that function.



main(List<String> arguments){
  ispossible();
  ispossible();
}
  void ispossible(){
    print("It's possible.");
  }
  void ispossible(){
    print("It's not possible.");
  }

Let's see if the result differs if we avoid void()


main(List<String> arguments){
  isTrue();
  isFalse();
}
   isTrue(){
    print("It's true.");
  }
   isFalse(){
    print("It's false.");
  }

This gives us the same result because according to the type of the value, it is automatically inferred. Here Dart knows they, here the types, are void because we are not using any return statements.


So, type annotations do not matter in such cases, but for API building (we will cover this part at the end of this book), type annotation is necessary.


Calling Function within Function

Lets see how to call a function within another function with arguments.


main(List<String> arguments) {
  myName();
}
myName(){
  print("You reached @NintyZeros.com");
  myAge(12);
}
myAge(int age){
  print("My age is ${age}");
}

//output
You reached in NintyZeros.com
My age is 12

Working with Return 

Lets see with an example how to use a return from a function.



void withoutReturningValue(){
  print("We cannot return any value from this function.");
}
int anIntegerReturnTypeFunction(){
  int num = 10;
  return num;
}
//using Fat Arrow
String stringReturnTypeFunction(String name, String address) => "This is $name and this is $address and we have used the Fat Arrow method.";
main(){
  withoutReturningValue();
  var returningInteger = anIntegerReturnTypeFunction();
  print("We are returning an integer: $returningInteger");
  print(stringReturnTypeFunction("John", "Jericho Town"));
}
Conclusion:

This should give a basic overview of how to work with a function in Dart. We will also see how to implement a basic object in Dart.

Hey I'm Venkat
Developer, Blogger, Thinker and Data scientist. nintyzeros [at] gmail.com I love the Data and Problem - An Indian Lives in US .If you have any question do reach me out via below social media