As Dart docs describe
it, as this being purely OOP language, Functions
are also object
.
This can be done in JS like this :
function functionReturningFunctionJS() {
return function functionReturnedByFunctionJS() {
return "This is function returned by function";
}
}
But I could not return function from function like this n dart:
Function functionReturningFunctionDart() {
return functionReturnedByFunctionDart(){
return "This is function Returned By function";
}
}
What is the right way to do it?
The function's return type is string. The function returns a string value to the caller. This is achieved by the return statement. The function test() returns a string.
When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.
Dart doesn't support multiple return values. or if you want the values be typed use a Tuple class like the package https://pub.dartlang.org/packages/tuple provides. See also either for a way to return a value or an error. +1 for tuple which does most closely what Go does with its built-in.
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.
Please refer the below add function which returns the another function (or closure).
void main() {
Function addTen = add(10);
print(addTen(5)); //15
print(add(10)(5)); //15
}
Function add(int a) {
int innerFunction(b) {
return a + b;
}
return innerFunction;
}
With anonymous function:
void main() {
Function addTen = add(10)
print(addTen(5)); //15
print(add(10)(5)); //15
}
Function add(int a) {
return (b) => a + b;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With