Types of Functions
Now its time to discuss the types of functions dart provide:
main() function
It is the first function where the controller starts the execution of code. The controller first enters the main function and then gets redirected to the other functions.
Code:
void main() {
print("I'm the controller, and I'm inside the main function");
}
Output:
I'm the controller, and I'm inside the main function
Functions with no argument and no return type
In this type of function, we neither provide any parameters to the function nor expect any return value from the function.
Example: We illustrate a function with no argument and return type.
Code:
void fun(){
//return type is void.
print("Functions with no argument and no return type");
}
void main() {
print("You are reading about:");
//calling the function.
fun();
}
Output:
You are reading about:
Functions with no argument and no return type
Explanation: Function fun() does not take any arguments and has no return type.
Functions with argument and no return type
In this type of function, we provide some parameters to the function but do not expect any return value from the function.
Example: We illustrate function with argument and no return type
Code:
void fun(String name, String teach){
//return type is void.
//name variable and teaches variable get initialized with the values passed.
print("Hi, my name is $name, and I teach $teach.");
}
void main() {
print("Inside the main function");
//calling the function with arguments.
fun("Ninja1","WebD");
fun("Ninja2", "Android");
fun("Ninja3", "ML");
}
Output:
Inside the main function
Hi, my name is Ninja1, and I teach WebD.
Hi, my name is Ninja2, and I teach Android.
Hi, my name is Ninja3, and I teach ML.
Explanation: Function fun() has two arguments which this function used as a variable to print different names and teachings.
Passing arguments with a default value: Sometimes, we need to give a default value to the function's argument so that it gets initialized with that default value if we have not passed any value corresponding to that argument.
Example: In this example, we show an argument having a default value.
Code:
void fun(String name, {String teach="Default Subject"}){
//this is how we provide default value to the parameter.
//return type is void.
print("Hi, my name is $name, and I teach $teaches.");
}
void main() {
print("Inside the main function");
//calling the function with arguments.
fun("Ninja1");
fun("Ninja2",teach: "Android");
fun("Ninja3");
}
Output:
Inside the main function
Hi, my name is Ninja1, and I teach Default Subject.
Hi, my name is Ninja2, and I teach Android.
Hi, my name is Ninja3, and I teach Default Subject.
Explanation: Function fun() has one default value argument, which means if we do not pass anything for the 'teach' argument, then it will have a default value as 'Default Subject'.
Functions with no argument but with return type
In this type of function, we do not provide any parameters to the function, but the function returns a value.
Example: We illustrate a function with no argument but with a return type.
Code:
String fun(){
//function with non void return type
//returning a string
return "Hello Everyone, My name is";
}
void main() {
print("Inside the main function");
//calling the function with return type.
print("${fun()} Ninja1.");
print("${fun()} Ninja2.");
print("${fun()} Ninja3.");
}
Output:
Inside the main function
Hello Everyone, My name is Ninja1.
Hello Everyone, My name is Ninja2.
Hello Everyone, My name is Ninja3.
Functions with argument and with return type
In this type of function, we provide some parameters to the function, and also, the function returns a value.
Example: We illustrate a function with arguments and with the return type.
Code:
int additionOf(int a, int b){
//This function have return type of int
//Returning the addition of a and b.
return a + b;
}
void main() {
int a = 10;
int b = 20;
int result = additionOf(a,b);
//Storing the value returned by the function
print("Addition of a and b: $result");
}
Output:
Addition of a and b: 30
Explanation: We are passing two integer arguments and returning their addition.
Recursive Functions
The function that refers to itself calls itself for execution until a base condition arrives is called a recursive function. The recursive function increases the readability of code. You can best use the concept of recursive function when you need to call a similar function but with varying parameters.
Code:
void reversePrint(int n){
//Base condition
if(n == 0)
return;
print(n);
//Calling the function Recursively
reversePrint(n-1);
}
void main() {
//Calling the recursive function for 1st time.
//count(n) function print n integer in reverse order.
reversePrint(10);
}
Output:
10
9
8
7
6
5
4
3
2
1
Lambda Functions
The Lambda function is a simple way of expressing a small function. It is also called the arrow function. Unlike the other functions, the lambda function does not have blocks of code. It has only one line, which is the return expression.
Example: We illustrate a lambda function that adds two integers.
Code:
int add(int a, int b) => (a + b);
void main() {
//Program to add first n integers.
int n = 10;
int res = 0;
for(int i=1; i<=n; i++){
//Adding using the lambda function
res = add(res,i);
}
print(res);
}
Output:
55
Explanation: Using the lambda function, we add integers from 1 to 10. Here, we run a for loop ten times and store the addition result to 'res' at every iteration.
Also see - Difference between argument and parameter
Frequently Asked Questions
How is the function call implemented?
A function call is implemented using stack. Each time a function calls another function, an entry is pushed onto the stack. The return address that the called function must return to the calling function is contained in this item, which is known as a stack frame or an activation record.
Why is a base condition necessary for a recursive function?
A way to return without performing a recursive call is the base case. In other words, it's the technique that prevents a never-ending cycle of recursive calls.
What does the arrow signify in the lambda function?
It signifies that the expression on its right side is the return statement by that lambda function. This means that expression is the operation performed by that function.
Conclusion
In this article, we've extensively discussed the Functions and their types in dart and implementation. We discussed all the functions in the dart and how to pass parameters and take the return value.
We hope that this blog has helped you enhance your knowledge regarding the Functions of Dart. We can use the concepts of Java in building an android app to learn in-depth about android development. Check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow.
Happy Coding!