Recursive Function in Dart
The recursive function is those functions in which function calls itself. It is a good way to avoid repeatedly calling the same function to get the output.
Example: Recursive function for fibonacci series.
/// Computes the nth Fibonacci number.
int fibonacci(int n)
{
// This is recursive function as it calls itself
return n < 2 ? n : (fibonacci(n - 1) + fibonacci(n - 2));
}
void main()
{
// input
var i = 20;
print('fibonacci($i) = ${fibonacci(i)}');
}
Output:
For input as 20
fibonacci(20) = 6765
Dart – Functions
The function is a set of statements that take inputs, do some specific computation and produces output. Functions are created when certain statements are repeatedly occurring in the program and a function is created to replace them. Functions make it easy to divide the complex program into smaller sub-groups and increase the code reusability of the program.
Contact Us