What is the default return value of a Dart function? What is the default return value of a Dart function? dart dart

What is the default return value of a Dart function?


In Dart each function without an explicit return someValue; returns null;

The null object does not have a method 'call'.

makeAdder (add2) without return returns null and null(3) leads to the exception.


I would like to quote two important note here. It might help others:

  1. Though Dart is Optionally typed ( meaning, specifying the return type of a function such as int or void is optional ), it always recommended to specify type wherever possible. In your code, as a sign of good programming practice, do mention the return type.

  2. If your function does not return a value then specify void. If you omit the return type then it will by default return null.

    All functions return a value. If no return value is specified, the statement return null; is implicitly appended to the function body.