What are the functions in Dart (Class 16)

What are the functions in Dart

Now we are going to start the most important topic of Dart i.e. What are the functions in Dart? Let’s start

What are the functions of Dart

“A block of code” which we can execute multiple times is called a function. We can create unlimited functions but every Function name should be different. It has many benefits including
 – Code Re-usability
 – Code efficiency
 – Good Performances
 – Code organization

 

What are Dart functions in Flutter?

Functions are the backbone of Flutter. The other main topics of constructors and the syntax of all widgets in Flutter are also dependent on functions. Briefly, 70-80% of syntax will be based on the concept of the function in Flutter.

 

What is the difference between function and method in dart?

In Java language we use methods but these methods are replaced with functions in Dart. An easy way to know the functions in any programming language is that there are parentheses after every function.

 

Function Syntax

An example of simple dart function syntax is:

abc (){
  print (“I live in Pakistan”);
}

Now let’s discuss its syntax which is as under:-
 – function name (mandatory)
 – parenthesis (mandatory)
 – function body (mandatory)
 – return type (optional)
 – parameters (optional)

 

How to name function

The same naming variables rules apply here. For example, to name function use camel case without space and no number at start. We should name functions in such a way as to fully understand them. As from data, fetchData is more suitable for understanding it. We add all related tasks in that function. We can also use variable, print, etc. commands in a function as per our requirement. Let’s check some examples:

abc (){

  print (“hello Nokia”);

}

userLogin(){

  print (“User information is correct”);

}

fetchData(){

  print (“No Data Available”);

}

getProducts(){

  print (“There are 250 products”);

}

Task: Make a function in Dart Pad.

 

Function calling:

It is important to know how to call a function. Before this, we will understand why to call a function. For example, we need a button 14 times in an app. For this purpose, we need to make 14 buttons. It will take much time with multiple lines of code. But using function calling, we just need to make a function and call it unlimited times. It is called code reusability.  We can call one function into another function body. In simple words, without calling a function, it is useless. We can call any function in the main function or any other function too. Its format is pretty simple just type the name of that function, add a parenthesis, and then semi-colon like this:

userLogin();

 

Execution path:

When we call a function, there is a process to run it which is called the execution path. Our compiler first searches for the main function which is also a built-in function and automatically calls. The execution starts from wherever the main function is built in Dart. The green arrow in Dart shows our main function. After the main function, it executes other functions in a sequence. The execution starts and ends on the same point i.e. main function.

 

Return type function

Some assignments end after completion whereas some need to return something before completion. The same happens in functions with return keywords. When we want a function should return a specific type of data then we use return. But a function can have a return keyword only once. The lines after the return keyword will never execute. A function will exit at the return keyword. Examples of return functions are as follows:-

double divNum() {
  int a = 30, b = 4;
  double c = a / b;
  return (c);
}
main() {
  double result = addNum();
print (result);
}

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.