Introduction
Hi folks, In this article, we discuss functions in flutter 3. If you’re new to flutter please read my previous articles. Functions are most important for any programming language. In this article, I explain a very simple way to learn flutter functions. Before we learn about functions you need some clarification about functions. So let’s start.
Functions in Flutter 3🤔
Functions are nothing but a piece of organized code for performing a single task. Functions are used for code reusability and modularity. Basically, functions are separated into two parts: User-Defined Functions and Predefined Functions.
Predefined Functions
Predefined Functions are nothing but some set of subroutines already coded into a programing language. We just call it and use the function in our code. We already use thousands of predefined functions in our daily code. For Example main(), print() and so on.
User-Defined Functions
A user-defined function is nothing but a piece of code to perform a specific task or a set of sub-routines created by a developer's wish. One of the powerful features of function is code reusability. For example
void main() {
//Declare Variables
var articleName = "Flutter Functions";
var authorName = "Navinprakash";
var publishingTime = "08:00 PM";
//Function Call and Passing values
articleDetails(articleName, authorName, publishingTime);
}
//Write Function with Parameters
void articleDetails(String articleName, String authorName, String publishingTime) {
print("Article Name: $articleName");
print("Author Name: $authorName");
print("Publishing Time: $publishingTime");
}
void main() {
//Declare Variables
var articleName = "Flutter Functions";
var authorName = "Navinprakash";
var publishingTime = "08:00 PM";
//Function Call and Passing values
articleDetails(articleName, authorName, publishingTime);
//Code Reusability
articleDetails('Flutter Function in Deep', 'C-Sharpcorner', '08:00 PM');
}
//Write Function with Parameters
void articleDetails(String articleName, String authorName, String publishingTime) {
print("Article Name: $articleName");
print("Author Name: $authorName");
print("Publishing Time: $publishingTime");
}
void main() {
//Declare Variables
var articleName = "Flutter Functions";
var authorName = "Navinprakash";
var publishingTime = "08:00 PM";
//Function Call and Passing values
final detail = articleDetails(articleName, authorName, publishingTime);
//Code Reusability
final detailTwo = articleDetails('Flutter Function in Deep', 'C-Sharpcorner', '08:00 PM');
print(detail);
print(detailTwo);
}
//Write Function with Parameters
String articleDetails(String articleName, String authorName, String publishingTime) {
return "Article Name: $articleName, Author Name: $authorName, Publish: $publishingTime";
}
Named Parameters
It’s also similar to normal functions but with only one change in the above function code snippet I pass simple arguments so the above functions are enough but you have a large number of arguments that need to be managed. I prefer to use named parameters for handling many arguments.
int findTheVolume(int length, {
int height,
int breadth
}) {
return length * height * breadth;
}
void main() {
var result1 = findTheVolume(10, height: 20, breadth: 30);
var result2 = findTheVolume(10, breadth: 30, height: 10);
print(result1);
print(result2);
}
Mini Function (Arrow Operator ‘=>’ ) 😆
Don’t worry it’s another way to write functions. While using the arrow operator your function looks more precise. For example.
void main() {
var a = 10;
var b = 5;
final addition = add(a, b);
final subtraction = sub(a, b);
print(addition);
print(subtraction);
}
//Normal Function
int add(int a, int b) {
return a + b;
}
// Function with Arrow Operator
int sub(int a, int b) => a - b;
Conclusion
In this article we discussed Function. If you have any queries please comment below. I will clear your queries. In the next article we will discuss classes it’s also a more important topic for all Object-oriented Programming languages. Thank you