Search code examples
functiondartdifferencereturn-type

What is the difference between void function and empty return type functions in Dart lang?


What is the difference between these two functions in Dart?

void _someFunction() {
  // some logic here
}

_someOtherFunction() {
  // some other logic here
}

So basically my question is that is there some kind of difference between these two functions?


Solution

  • If you don't provide a return type the return type is assumed to be dynamic rather than void. A function that returns void doesn't return a value. Whereas a function that returns dynamic could return anything, but not in a way that is particularly type safe.

    For example

    void _someFunction() {
      // some logic here
      return 10; // compile-time error
    }
    
    _someOtherFunction() {
      // some other logic here
      return 10; // no compile-time error
    }