Dart Recursion is the method where a function calls itself as its subroutine. It is used to solve the complex problem by dividing it into sub-part. A function which is called itself again and again or recursively, then this process is called recursion. The iterators can be an option to solve problems, but recursion is recommended to the programmers to deal with complex problems because it is an effective approach of problem-solving technique. It requires less time and code to evaluate the same complex task. Recursion makes many calls to the same function; however, there should be a base case to terminate the recursion. Recursion uses the divide and conquers technique to solve a complex mathematical computation task. It divides the large task into small chunks. Recursion is not recommended to solve all types of problems. However, it is best for a few questions such as searching, sorting, Inorder/Preorder/Postorder, Tree Traversal, and DFS of Graph algorithms. But, while using recursion, it must be implemented carefully; otherwise, it turns into the infinite loop. What is base condition in recursion? In the above example, the base case is defined as n<=1, and a larger value of a number can be solved by changing to a lesser one till the base case is matched. Note – The base case or valid terminating condition is required in recursion function; otherwise, it will turn into an infinite loop. Dart Recursive Function Recursive functions are quite similar to the other functions, but difference is to calling itself recursively. A recursive function repeats multiple times until it returns the final output. It allows programmers to solve complex problems with minimal code. How does recursion works? Let’s understand the concept of the recursion of the example of a factorial of a given number. In the following example, we will evaluate the factorial of n numbers. It is the series of the multiplication as follows. Characteristics of Recursive Function The characteristics of the recursive function are given below. Let’s have a look at recursion syntax: Syntax: Let’s understand the following example. Example – 1 Output: Explanation: In the above example, the factorial() is a recursive function as it call itself. When we called the factorial() function by passing the integer value 5, it will recursively call itself by decreasing the number. The factorial() function will be called every time until it matched the base condition, or it is equal to one. It multiplied the number with factorial of the number. Consider the following explanation of the recursive call. The recursion is ended when the number reduced to 1, and it is the base condition of recursion. A recursion function must have a base condition to avoid to infinite call. Disadvantage of Recursion
The main() function
The main() function is the top-level function of the Dart. It is the most important and vital function of the Dart programming language. The execution of the programming starts with the main() function. The main() function can be used only once in a program. It is responsible for all types of execution such as user-defined statements, functions, and libraries function. The program begins with main() function and we declares variable, and user defined executable statements inside it. The main function returns void and can have an optional List<String> parameter as arguments. The general syntax of the main() function is given below. Syntax: Example – 1 Output Dart Return Value Sometimes the function returns a value after evaluating the function statements to the point where it is called from. The return statement holds the result of the function, and it is transferred to the function call. The return keyword is used to represent the return statement. If the return statement not specified, then the function returns null. The return statement is optional to specify in function, but there can be only one return statement in a function. Syntax: Dart value with Return Value Below is given syntax of return value. Syntax: Here is the description of the above syntax. function_name – It represents the function name, which can be any valid identifier. return type – It denotes the return type of the function. It can be any valid data type. The return must be matched with the return type of the function. Let’s understand the following example – Example – Output
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. The syntax is as follows. Syntax: Let’s consider the following example. Example – Output: Explanation: In the above example, we defined an anonymous function with an untype argument item. The function called for each item in the list and printed the strings with its specified index value. If the function consists of one statement, then we can also write the above code in the following way. It is equivalent to the previous code. You can verify it by paste in your dart pad and run. Lexical Scope As we have discussed in the Dart introduction, it is a lexical scope language which means the variable’s scope is decided at compile-time. The scope of the variable is determined when code is compiled. The variable behaves differently if they defined in the different curly braces. Let’s understand the following example. Example – Observe the above code, the nestedFunction() used the variables of the previous function. Lexical Closure A lexical closure is referred to as a closure, is a function object that has access to variables in its lexical scope even when the function is used of its original scope. In other words, it provides access to an outer function’s scope from inner function. Let’s understand the following example. Example – Output Explanation: In the above code, the initial() function created a local variable called name and function called disp_Name(). The disp_Name() function defined inside the initial() function and hence disp_Name() function has no local variable its own. The inner function can access the variable of the outer functions. The function disp_Name() can access the name variable which is declared in the outer function, initial().
Function
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 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’t need to write code for adding, subtracting, multiplying, and divide again and again. We can use the functions multiple times by calling. 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. Advantages of Functions The few benefits of the Dart function is given below. Let’s understand the basic concept of functions. Defining a Function 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. Syntax: Let’s understand the general syntax of the defining function. Let’s understand the following example. Example – 1 Calling a Function After creating a function, we can call or invoke the defined function inside the main() function body. A function is invoked simply by its name with a parameter list, if any. The syntax is given below. Syntax: Note – Calling function must be ended with semicolon (;). 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 : Passing Arguments to Function 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. Actual Parameter – A parameter which is passed during a function definition is called the actual parameter. Formal Parameter – A parameter which is passed during a function call is called the formal parameter. We will learn more about the parameter in the next tutorial. Return a Value from Function A function always returns some value as a result to the point where it is called. The return 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. Syntax: Example – Function Examples Let’s understand the functions by using a program of adding two numbers using functions. Dart Function with parameter and return value In the following example, we are creating a sum() function to add two number. Example – 1 Output Explanation: In the above example, we declared a function named sum() and passed two integer variables as actual parameters. In the function body, we declared a result variable to store the sum of two numbers and returned the result. In order to add two values, we called a function with the same name, passed formal parameters 30 and 20. The sum() returned a value which we stored in the variable c and printed the sum on the console. Dart Function with No Parameter and Return Value 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. Syntax: Let’s understand the following example. Example – 2 Output Explanation: In the above example, we created a function named greetings() 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. Dart Function with No Parameter and without a Return Value We can declare a function without parameter and no return value. The syntax is given below. Syntax: In the above general syntax- void – It represents the function has no return type. fun_name – It represents the function name. Let’s understand the following example. Example – 3 Output Explanation: In the above example, we created a function called greeting() outside the main() function and writing the print statement. Inside the main() function, we called the defined function and printed the output to console. Dart Function with Parameter and without a Return Value We are creating a function to find the given number is even or odd. Let’s understand the following example. Example – 4 Output
Boolean
Dart Boolean data type is used to check whether a given statement true or false. The true and false are the two values of the Boolean type, which are both compile-time constants. In Dart, The numeric value 1 or 0 cannot be used to specify the true or false. The bool keyword is used to represent the Boolean value. The syntax of declaring the Boolean variable is given below. Syntax – Consider the following example. Example – Output: Explaination – We have declared the bool variable check that will use to verify the given expression. The expression 20>12 returned the true and we printed the result.
do while Loop
Dart do while loop executes a block of the statement first and then checks the condition. If the condition returns true, then the loop continues its iteration. It is similar to Dart while loop but the only difference is, in the do-while loop a block of statements inside the body of loop will execute at least once. Dart do-while loop Flowchart The syntax is given below. Syntax: Here, the block of statement which is inside the do while body will executes first then evaluates the given condition. A condition is evaluated either Boolean true or false. If it returns true, then the statements are executed again, and the condition is rechecked. If it returns false, the loop is ended, and control transfers to out of the loop. Let’s understand the following example. Example: Output: Explanation – In the above code, we have initialized the variable i with value 10. In do-while loop body, we defined two statements. In the first iteration, the statement printed the initial value of i and increased by 1. Now the value of i becomes 11 and then we checked the condition. The condition is, the value of i must be less than or greater than the 20. It matched with the condition and loop moved to the next iteration. It printed the series of numbers 10 to 20 until the condition returned false.
While Loop
The while loop is used when the number of execution of a block of code is not known. It will execute as long as the condition is true. It initially checks the given condition then executes the statements that are inside the while loop. The while loop is mostly used to create an infinite loop. Dart While Loop Flowchart The syntax is given below. Syntax: Here, if the condition returns the true, then the loop body is executed and condition is evaluated again. If the condition returns false then the loop is terminated and the control transfer to out of the loop. Let’s understand the following example. Example – 1 Output: Explanation: Above example, we initialized the integer variable i with value 1 respectively, in the next statement we have defined while loop, that check the condition that, the value of i is smaller or greater than 5 in each iteration. If the condition returns true then while loop body is executed and the condition is rechecked. It will be continued until the condition is false. After that, the value of i is 6 that violated the condition; then, the loop is terminated. It printed the sequence of 1 to 5 on the console. Infinite While Loop When the while loop executes an endless time is called infinite while loop. Let’s have a look at the infinite loop example. Example – We have made only one change in above code. We reduced the value of i for each iteration of while loop. So it will never match with the specified condition and became an infinite loop. Example – 2 It will print the given statement for infinite time. When we declare Boolean true in while loop, then it automatically became an infinite loop. Logical Operator while loop Sometimes we need to check the multiple conditions in a while loop. We can do this by using logical operators such as (||, &&, and !). Let’s see the following concepts. Consider the following example. Example – Output: Explanation: In the above code, we have assigned two variables n1 and n2 with the value 1 in both. Now we checked multiple conditions in the while loop where n1 is less than or equal to 4 and n2 is less than or equal to 3. In the first iteration, it checked both values and printed the result. At one point, when the value of n1 and n2 is equal to 4. The n1 satisfied the condition one, but n2 did not meet the second condition, so the loop is terminated and printed the result to the screen.
Dart for..in Loop
The for..in loop is similar to for loop but different in its syntax. It iterates through an object’s properties. The Dart for..in loop accepts an expression as iterator and iterates through the elements one at a time in sequence. The variable var holds the values of the iteration. The for…in will execute until elements remain in iterators. Dart For In Loop Flow Diagram The syntax is given below. Syntax: Let’s understand the following example. Example – Output: Explanation: In the above program, we have iterator list1 and variable i. In the first iteration of the loop, the first element of the list assigned to the variable i. This process happened again, and the second element of the list assigned to i. It will be continued until there is no element left in the list. It printed the all element of the list to the console. The for..in loop is suitable to iterate over the Dart object such as list, map, and set, where for loop is more effective to iterate along with the specified condition. Let’s understand another example. Example – Output: Explanation: In the above example, we have declared a variable sum with value 0. We have a for..in loop with the iterator list, and each element of list added to the sum variable after each iteration. In the first iteration, the value of the sum is equal to 10. In the next iteration, the value of sum became 30 after added 20 to it. After complete the iteration, it returned the sum of all elements of the list.
Dart for Loop
Dart for loop is used when we familiar with the number of execution of a block of code. It is similar to the C, C++, and Java for loop. It takes an initial variable to start the loop execution. It executes a block of code until it matches the specified condition. When the loop is executed, the value of iterator is updated each iteration, and then the test-expression is evaluated. This process will continue until the given test-expression is true. Once the test-expression is false, the for loop is terminated. Dart for Loop Flowchart Syntax: Let’s understand the following example. Example 1: Output: Explanation: In the above example, we have initialized an integer variable i as initial value. We have assigned 1 to the variable and in the conditional part, we have defined the loop executed until value of i is smaller or equal to 10. Each time the loop will be iterated it will be increased value by 1. In the first iteration of a loop, the value of i is incremented by 1 and it will become 2. Now the condition is rechecked if condition is true, then the loop will be moved on next iteration. The iteration of the loop will continued until the value becomes 10. We can skip the initial value from the for loop. Consider the following example. It will give the same output as previous code. Also, we can skip the condition, increment, or decrement by using a semicolon. Nested for Loop The nested for loop means, “the for loop inside another for loop”. A for inside another loop is called an inner loop and outside loop is called the outer loop. In each iteration of the outer loop, the inner loop will iterate to entire its cycle. Let’s understand the following example of nested for loop. Example – Output: Let’s understand the working of nested for loop. Example – 2 Understand the inner loop cycle Output: Observe the above code, we have defined the working of the inner loop. The inner loop will be repeated for each iteration of the outer loop.
Loops
Dart Loop is used to run a block of code repetitively for a given number of times or until matches the specified condition. Loops are essential tools for any programming language. It is used to iterate the Dart iterable such as list, map, etc. and perform operations for multiple times. A loop can have two parts – a body of the loop and control statements. The main objective of the loop is to run the code multiple times. Dart supports the following type of loops. We describe a brief introduction to the dart loops as follows. Dart for loop The for loop is used when we know how many times a block of code will execute. It is quite same as the C for loop. The syntax is given below. Syntax – The loop iteration starts from the initial value. It executes only once. The condition is a test-expression and it is checked after each iteration. The for loop will execute until false returned by the given condition. The incr/decr is the counter to increase or decrease the value. Let’s understand the following example. Example – Output: Dart for… in Loop The for…in loop is slightly different from the for loop. It only takes dart object or expression as an iterator and iterates the element one at a time. The value of the element is bound to var, which is and valid and available for the loop body. The loop will execute until no element left in the iterator. The syntax is given below. Syntax – Example : Output: We need to declare the iterator variable to get the element from the iterator. Dart while loop The while loop executes a block of code until the given expression is false. It is more beneficial when we don’t know the number of execution. The syntax is given below. Syntax: Let’s understand the following example. Example – Output: Dart do…while Loop The do…while loop is similar to the while loop but only difference is that, it executes the loop statement and then check the given condition. The syntax is given below. Syntax – Example – Output: Selection of the loop The selection of a loop is a little bit difficult task to the programmer. It is hard to decide which loop will be more suitable to perform a specific task. We can determine the loop based on the following points.