Dart Programming - Returning Function


Advertisements

Functions may also return value along with the control, back to the caller. Such functions are called as returning functions.

Syntax

return_type function_name(){  
   //statements  
   return value;  
}
  • The return_type can be any valid data type.

  • The return statement is optional. I not specified the function returns null;

  • The data type of the value returned must match the return type of the function.

  • A function can return at the most one value. In other words, there can be only one return statement per function.

Example

Let’s take an example to understand how returning functions work.

  • The example declares a function test(). 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. This is displayed as output.

void main() { 
   print(test()); 
}  
String test() { 
   // function definition 
   return "hello world"; 
}

It will produce the following output

hello world 
dart_programming_functions.htm
Advertisements