BASH/C3/Recursive-function/English-timed
From Script | Spoken-Tutorial
Revision as of 15:07, 12 December 2014 by PoojaMoolya (Talk | contribs)
Time | Narration |
00:01 | Dear friends, welcome to the spoken tutorial on Recursive function. |
00:07 | In this tutorial, we will learn |
00:10 | What is a Recursive function? |
00:12 | With the help of some examples |
00:15 | To follow this tutorial, you should have knowledge of Shell Scripting in BASH. |
00:20 | If not, for relevant tutorials please visit our website which is as shown,http://www.spoken-tutorial.org |
00:27 | For this tutorial I am using |
00:29 | Ubuntu Linux 12.04 Operating System |
00:33 | and GNU BASH version 4.2 |
00:37 | Please note, GNU Bash version 4 or above is recommended for practice |
00:44 | Let us see what a recursive function is. |
00:48 | A recursive function is one which calls itself |
00:52 | Recursion is a useful technique for simplifying complex algorithms |
00:59 | Let me open a file named factorial.sh |
01:04 | I have typed the code in this file. |
01:07 | This is the shebang line. |
01:10 | factorial is the function name. |
01:12 | Inside this, we print a message “Inside factorial function” |
01:19 | This statement reads user's input and stores the value in variable 'n' |
01:26 | Here we have if-else condition. |
01:30 | If condition checks whether the value of 'n' is equal to zero. |
01:36 | If true, it will display the message "factorial value of n is 1". |
01:42 | Here is the else part of the if statement. |
01:46 | It calls the factorial function. |
01:50 | And fi is the end of the if-else statement. |
01:55 | Let us run the file factorial.sh. |
01:59 | Open the terminal using CTRL+ALT+T keys simultaneously on your keyboard. |
02:07 | Type: chmod space plus x space factorial dot sh |
02:15 | Press Enter. |
02:17 | Type dot slash factorial.sh |
02:21 | Press Enter. |
02:24 | We see "Enter the number". |
02:26 | I will enter 0. |
02:29 | The output is displayed as: |
02:31 | factorial value of 0 is 1 |
02:35 | Now press the uparrow key. Recall the previous command. |
02:40 | Press Enter. |
02:42 | This time, I will enter 5 . |
02:45 | Now the output is displayed as: |
02:47 | Inside factorial function. |
02:51 | Let us add some more logic to the factorial function. |
02:56 | We will calculate the factorial of a number. |
03:01 | Come back to our code. |
03:03 | Now let us replace, the echo statement inside the factorial function with the code block. |
03:10 | Click on Save |
03:13 | temp is a variable and stores the value entered by user. |
03:19 | If condition checks whether the variable value is equal to 1. |
03:25 | If true, it will print 1. |
03:29 | This is the else part of the if statement. |
03:33 | This reduces one from the temp variable value. |
03:37 | And stores the result in a variable 'f'. |
03:42 | Variable f stores the output of factorial function. |
03:46 | This is a recursive call. |
03:50 | Value of variable f and temp is multiplied and stored in f. |
03:57 | Then we print the value of f. |
04:00 | End of if-else statement and function. |
04:05 | Now come back to our slides. |
04:08 | Let us understand the flow of the program. |
04:12 | # The value of n is taken from the user i.e. n |
04:17 | # If the value entered is equal to zero, then it prints a message |
04:24 | # Else it goes to the function factorial |
04:29 | # Here, if the value is equal to one, then it prints value as one |
04:36 | # If not, it makes a recursive call until the value is equal to one |
04:44 | # Then, all the values are multiplied and displayed |
04:49 | Now come back to our terminal. |
04:52 | Press the uparrow key. |
04:54 | Recall the previous command ./factorial.sh |
04:58 | Press Enter. |
05:00 | Now I will enter 5 as the input value. |
05:05 | We get the factorial of number 5. |
05:08 | That is 120. |
05:11 | We can see the flow of the program on terminal. Analyse and trace the flow of the program. |
05:18 | Come back to our slides. |
05:20 | Let us summarise. |
05:21 | In this tutorial we have learnt, |
05:23 | Recursive function |
05:25 | With the help of some examples |
05:28 | As an assignment. |
05:29 | Write a program where the recursive function calculates the sum of N numbers |
05:36 | Watch the video available at the link shown below. |
05:39 | It summarises the Spoken Tutorial project. |
05:43 | If you do not have good bandwidth, you can download and watch it. |
05:47 | The Spoken Tutorial Project Team Conducts workshops using spoken tutorials |
05:53 | Gives certificates to those who pass an online test |
05:58 | For more details, please write to contact@spoken-tutorial.org |
06:06 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
06:10 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
06:18 | More information on this Mission is available at the link shown below http://spoken-tutorial.org\NMEICT-Intro |
06:24 | The script has been contributed by FOSSEE and Spoken-Tutorial teams. |
06:29 | This is Ashwini Patil signing off. |
06:33 | Thank you for joining. |