Day 5 - Functions
Day 5 - Functions
When we call a function, the control is transferred to the called function. Then
the called function executes all defined statements and returns the result to
the calling function. The control returns to the main() function.
Example:
mul(10,20);
Anonymous Function
We have learned the Dart Function, which is defined by using a user-define
name. Dart also provides the facility to specify a nameless function or function
without a name. This type of function is known as an anonymous function,
lambda, or closure. An anonymous function behaves the same as a regular
function, but it does not have a name with it. It can have zero or any number
of arguments with an optional type annotation.
We can assign the anonymous function to a variable, and then we can retrieve
or access the value of the closure based on our requirement.
An Anonymous function contains an independent block of the code, and that
can be passed around in our code as function parameters.
Syntax:
(parameter_list) {
statement(s)
}
Example:
void main() {
var list = ["James", "Patrick", "Mathew", "Tom"];
print("Example of anonymous function");
list.forEach((item) {
print('${list.indexOf(item)}: $item');
});
}
Nested Functions
void main() {
print("Main");
myFunction();
}
void myFunction() {
print("MyFunction");
void nestedFunction() {
print("NestedFunction");
}
nestedFunction();
}
Functions with Optional Parameter:
There are also optional parameter system in Dart which allows user to give
some optional parameters inside the function.
Sr.
No. Parameter Description
Optional Positional
1. Parameter To specify it use square (‘[ ]’) brackets
Optional parameter
3. with default values Here parameters are assign with default values.
function1(p1, [o1]) {
print("Param Value Is : ${p1} and Option Param Valus Is : ${o1}");
}
void main() {
function1(4);
function1(5, g2: 9);
}
Recursive Function
A recursive function is not much different from any other function, basically a
function calling itself directly or indirectly is known as recursive function. A
recursive function repeats itself several times, in order to compute or return
final output. Recursive functions are quite common in computer programing as
they allow programmers to write efficient programs with minimal code.
Characteristics of a Recursive function
• A recursive function is a function which calls itself.
• The speed of a recursive program is slower because of stack overheads.
• A recursive function must have terminating conditions or base case, and
recursive expressions.
Example:
int fact(int n) {
if (n > 1)
return n * fact(n - 1);
else
return 1;
}
main() {
var num = 5;
var f = fact(num);
print("Factorial Program Using Recursion.");
print("Factorial of 5 Is : ${f}");
}
void main() {
// 3 - pass the functions into the method
exec(10, plusOne);
exec(10, double);
}
Example:
// 1 - define a method that takes a function
void exec(int i, Function f) {
print(f(i));
}
void main() {
// 3 - pass the functions into the method
exec(10, (x) => x + 1);
exec(10, (x) => x * x);
}