{"id":3610,"date":"2022-05-21T06:20:01","date_gmt":"2022-05-21T06:20:01","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3610"},"modified":"2022-05-21T06:20:01","modified_gmt":"2022-05-21T06:20:01","slug":"dart-function","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/21\/dart-function\/","title":{"rendered":"Dart Function"},"content":{"rendered":"\n<p>Dart function is a set of codes that together perform a specific task. It is used to break the large code into smaller modules and reuse it when needed. Functions make the program more readable and easy to debug. It improves the modular approach and enhances the code reusability.<\/p>\n\n\n\n<p>Suppose, we write a simple calculator program where we need to perform operations number of times when the user enters the values. We can create different functions for each calculator operator. By using the functions, we don&#8217;t need to write code for adding, subtracting, multiplying, and divide again and again. We can use the functions multiple times by calling.<\/p>\n\n\n\n<p>The function provides the flexibility to run a code several times with different values. A function can be called anytime as its parameter and returns some value to where it called.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Functions<\/h2>\n\n\n\n<p>The few benefits of the\u00a0Dart\u00a0function is given below.<\/p>\n\n\n\n<ul><li>It increases the module approach to solve the problems.<\/li><li>It enhances the re-usability of the program.<\/li><li>We can do the coupling of the programs.<\/li><li>It optimizes the code.<\/li><li>It makes debugging easier.<\/li><li>It makes development easy and creates less complexity.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s understand the basic concept of functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining a Function<\/h2>\n\n\n\n<p>A function can be defined by providing the name of the function with the appropriate parameter and return type. A function contains a set of statements which are called function body. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return_type func_name (parameter_list):  \n{  \n    \/\/statement(s)  \n   return value;  \n}  <\/code><\/pre>\n\n\n\n<p>Let&#8217;s understand the general syntax of the defining function.<\/p>\n\n\n\n<ul><li><strong>return_type &#8211;<\/strong>&nbsp;It can be any data type such as void, integer, float, etc. The return type must be matched with the returned value of the function.<\/li><li><strong>func_name &#8211;<\/strong>&nbsp;It should be an appropriate and valid identifier.<\/li><li><strong>parameter_list &#8211;<\/strong>&nbsp;It denotes the list of the parameters, which is necessary when we called a function.<\/li><li><strong>return value &#8211;<\/strong>&nbsp;A function returns a value after complete its execution.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211; 1<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int mul(int a, int b){  \n     int c;  \n     c = a+b;  \n     print(\"The sum is:${c}\");  \n}  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Calling a Function<\/h2>\n\n\n\n<p>After creating a function, we can call or invoke the defined function inside the\u00a0main() function\u00a0body. A function is invoked simply by its name with a parameter list, if any. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun_name(&lt;argument_list>);  \nor  \nvariable = function_name(argument);  <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Note &#8211; Calling function must be ended with semicolon (;).<\/h4>\n\n\n\n<p>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..<\/p>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mul(10,20);  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Passing Arguments to Function<\/h2>\n\n\n\n<p>When a function is called, it may have some information as per the function prototype is known as a parameter (argument). The number of parameters passed and data type while the function call must be matched with the number of parameters during function declaration. Otherwise, it will throw an error. Parameter passing is also optional, which means it is not compulsory to pass during function declaration. The parameter can be two types.<\/p>\n\n\n\n<p><strong>Actual Parameter &#8211;<\/strong>&nbsp;A parameter which is passed during a function definition is called the actual parameter.<\/p>\n\n\n\n<p><strong>Formal Parameter &#8211;<\/strong>&nbsp;A parameter which is passed during a function call is called the formal parameter.<\/p>\n\n\n\n<p>We will learn more about the parameter in the next tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Return a Value from Function<\/h2>\n\n\n\n<p>A function always returns some value as a result to the point where it is called. The&nbsp;<strong>return<\/strong>&nbsp;keyword is used to return a value. The return statement is optional. A function can have only one return statement. The syntax is given below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/dart\/images\/dart-function.png\" alt=\"Dart Function\"\/><\/figure>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return &lt;expression\/values>  <\/code><\/pre>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return result;  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Function Examples<\/h2>\n\n\n\n<p>Let&#8217;s understand the functions by using a program of adding two numbers using functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dart Function with parameter and return value<\/h3>\n\n\n\n<p>In the following example, we are creating a sum() function to add two number.<\/p>\n\n\n\n<p><strong>Example &#8211; 1<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {  \n  print(\"Example of add two number using the function\");    \n  \/\/ Creating a Function  \n  \n  int sum(int a, int b){  \n            \/\/ function Body  \n            int result;  \n            result = a+b;  \n            return result;  \n}  \n\/\/ We are calling a function and storing a result in variable c  \nvar c = sum(30,20);  \nprint(\"The sum of two numbers is: ${c}\");  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example of add two number using the function\nThe sum of two numbers is: 50\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In the above example, we declared a function named&nbsp;<strong>sum()<\/strong>&nbsp;and passed two integer variables as actual parameters. In the function body, we declared a&nbsp;<strong>result<\/strong>&nbsp;variable to store the sum of two numbers and returned the result.<\/p>\n\n\n\n<p>In order to add two values, we called a function with the same name, passed formal parameters 30 and 20. The&nbsp;<strong>sum()<\/strong>&nbsp;returned a value which we stored in the variable c and printed the sum on the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dart Function with No Parameter and Return Value<\/h3>\n\n\n\n<p>As we discussed earlier, the parameters are optional to pass while defining a function. We can create a function without parameter return value. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return_type func_name()   \n{  \n       \/\/Statement(s);  \n       return value;  \n}  <\/code><\/pre>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211; 2<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main(){  \n\/\/ Creating a function without argument  \nString greetings(){  \n   return \"Welcome to Javapoint\";  \n}  \n\/\/ Calling function inside print statement  \nprint(greetings());  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Javapoint\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In the above example, we created a function named&nbsp;<strong>greetings()<\/strong>&nbsp;without argument and returned the string value to the calling function. Then, we called the greeting() function inside the print statement and printed the result to the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dart Function with No Parameter and without a Return Value<\/h3>\n\n\n\n<p>We can declare a function without parameter and no return value. The syntax is given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func_name() {  \n \/\/statement  \n}  \nOr  \nvoid fun_name() {  \n  \/\/statement(s)  \n}  <\/code><\/pre>\n\n\n\n<p>In the above general syntax-<\/p>\n\n\n\n<p><strong>void &#8211;<\/strong>&nbsp;It represents the function has no return type.<\/p>\n\n\n\n<p><strong>fun_name &#8211;<\/strong>&nbsp;It represents the function name.<\/p>\n\n\n\n<p>Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211; 3<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Creating a function without argument  \nvoid greetings()  \n{  \n   print(\"Welcome to JavaTpoint\");  \n}  \nvoid main() {  \n  print(\"The example of Dart Function\");  \n  \/\/ function callling  \n  greetings();  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The example of Dart Function\nWelcome to JavaTpoint\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In the above example, we created a function called&nbsp;<strong>greeting()<\/strong>&nbsp;outside the&nbsp;<strong>main()<\/strong>&nbsp;function and writing the print statement. Inside the&nbsp;<strong>main()<\/strong>&nbsp;function, we called the defined function and printed the output to console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dart Function with Parameter and without a Return Value<\/h3>\n\n\n\n<p>We are creating a function to find the given number is even or odd. Let&#8217;s understand the following example.<\/p>\n\n\n\n<p><strong>Example &#8211; 4<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-function#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()   \n{  \n  void number(int n){  \n           \/\/ Check the given number is even or odd  \n           if (n%2 ==0){  \n                   print(\"The given number is even\");  \n             }  \n           else {  \n                  print(\"The given number is odd\");  \n            }  \n}  \n   number(20);  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The given number is even<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dart function is a set of codes that together perform a specific task. It is used to break the large code into smaller modules and reuse it when needed. Functions make the program more readable and easy to debug. It improves the modular approach and enhances the code reusability. Suppose, we write a simple calculator [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[918],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3610"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3610"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3610\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}