Difference between revisions of "BASH/C3/Recursive-function/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Dear friends, welcome to the spoken tutorial on''' Recursive function.''' | + | | Dear friends, welcome to the '''spoken tutorial''' on''' Recursive function.''' |
|- | |- | ||
Line 13: | Line 13: | ||
|- | |- | ||
| 00:10 | | 00:10 | ||
− | | '''What is a Recursive''' function | + | | '''What is a Recursive''' function |
|- | |- | ||
| 00:12 | | 00:12 | ||
− | | | + | |with the help of some examples. |
|- | |- | ||
Line 25: | Line 25: | ||
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | If not, for relevant tutorials please visit our website which is as shown | + | | If not, for relevant tutorials, please visit our website which is as shown.'''http://www.spoken-tutorial.org''' |
|- | |- | ||
| 00:27 | | 00:27 | ||
− | | For this tutorial I am using | + | | For this tutorial, I am using |
|- | |- | ||
| 00:29 | | 00:29 | ||
− | | | + | | '''Ubuntu Linux''' 12.04''' '''Operating System |
|- | |- | ||
Line 41: | Line 41: | ||
|- | |- | ||
| 00:37 | | 00:37 | ||
− | | Please note,''' GNU Bash''' '''version 4''' or above is recommended for practice | + | | Please note,''' GNU Bash''' '''version 4''' or above is recommended for practice. |
|- | |- | ||
| 00:44 | | 00:44 | ||
− | | Let us see what a recursive function is. | + | | Let us see what a '''recursive function''' is. |
|- | |- | ||
| 00:48 | | 00:48 | ||
− | | A '''recursive function''' is one which calls itself | + | | A '''recursive function''' is one which calls itself. |
|- | |- | ||
| 00:52 | | 00:52 | ||
− | | '''Recursion''' is a useful technique for simplifying complex algorithms | + | | '''Recursion''' is a useful technique for simplifying complex algorithms. |
|- | |- | ||
| 00:59 | | 00:59 | ||
− | | Let me open a file named '''factorial.sh''' | + | | Let me open a file named '''factorial.sh'''. |
|- | |- | ||
| 01:04 | | 01:04 | ||
− | | I have typed the code in this file. | + | | I have typed the '''code''' in this file. |
|- | |- | ||
Line 69: | Line 69: | ||
|- | |- | ||
| 01:10 | | 01:10 | ||
− | | '''factorial '''is the function name. | + | | '''factorial '''is the '''function''' name. |
|- | |- | ||
| 01:12 | | 01:12 | ||
− | | Inside this, we print a message “Inside factorial function” | + | | Inside this, we print a message “Inside factorial function”. |
|- | |- | ||
| 01:19 | | 01:19 | ||
− | | This statement reads user's input and stores the value in variable | + | | This statement reads user's '''input''' and stores the value in variable 'n'. |
|- | |- | ||
| 01:26 | | 01:26 | ||
− | | Here we have '''if-else condition.''' | + | | Here, we have '''if-else condition.''' |
|- | |- | ||
| 01:30 | | 01:30 | ||
− | | '''If '''condition checks whether the value of | + | | '''If '''condition checks whether the value of 'n' is equal to zero. |
|- | |- | ||
| 01:36 | | 01:36 | ||
− | | If ''' | + | | If '''True, '''it will display the message '''"factorial value of n is 1".''' |
|- | |- | ||
Line 105: | Line 105: | ||
|- | |- | ||
| 01:55 | | 01:55 | ||
− | | Let us run the file''' factorial.sh.''' | + | | Let us '''run''' the file''' factorial.sh.''' |
|- | |- | ||
| 01:59 | | 01:59 | ||
− | | Open the terminal using '''CTRL+ALT+T '''keys simultaneously on your keyboard. | + | | Open the '''terminal''' using '''CTRL+ALT+T '''keys simultaneously on your keyboard. |
|- | |- | ||
Line 145: | Line 145: | ||
|- | |- | ||
| 02:35 | | 02:35 | ||
− | | Now press the ''' | + | | Now press the '''up-arrow''' key. Recall the previous '''command'''. |
|- | |- | ||
Line 157: | Line 157: | ||
|- | |- | ||
| 02:45 | | 02:45 | ||
− | | Now the output is displayed as: | + | | Now the '''output''' is displayed as: |
|- | |- | ||
Line 173: | Line 173: | ||
|- | |- | ||
| 03:01 | | 03:01 | ||
− | | Come back to our code. | + | | Come back to our '''code'''. |
|- | |- | ||
| 03:03 | | 03:03 | ||
− | | Now let us replace | + | | Now let us replace the '''echo statement''' inside the '''factorial function''' with the '''code block'''. |
|- | |- | ||
| 03:10 | | 03:10 | ||
− | | Click on '''Save''' | + | | Click on '''Save'''. |
|- | |- | ||
| 03:13 | | 03:13 | ||
− | | '''temp '''is a variable and stores the value entered by user. | + | | '''temp '''is a '''variable''' and stores the '''value''' entered by user. |
|- | |- | ||
| 03:19 | | 03:19 | ||
− | | '''If condition''' checks whether the variable value is equal to | + | | '''If condition''' checks whether the variable value is equal to '''1'''. |
|- | |- | ||
| 03:25 | | 03:25 | ||
− | | If ''' | + | | If '''True''', it will print '''1'''. |
|- | |- | ||
Line 201: | Line 201: | ||
|- | |- | ||
| 03:33 | | 03:33 | ||
− | | This reduces one from the '''temp '''variable value | + | | This reduces one from the '''temp '''variable value |
|- | |- | ||
| 03:37 | | 03:37 | ||
− | | | + | | and stores the result in a variable ''''f'.''' |
|- | |- | ||
| 03:42 | | 03:42 | ||
− | | '''Variable f '''stores the output of '''factorial function.''' | + | | '''Variable f '''stores the '''output''' of '''factorial function.''' |
|- | |- | ||
Line 221: | Line 221: | ||
|- | |- | ||
| 03:57 | | 03:57 | ||
− | | Then we print the value of '''f.''' | + | | Then we '''print''' the value of '''f.''' |
|- | |- | ||
Line 237: | Line 237: | ||
|- | |- | ||
| 04:12 | | 04:12 | ||
− | | # The value of '''n''' is taken from the user i.e. '''n''' | + | | # The value of '''n''' is taken from the user i.e. '''n'''. |
|- | |- | ||
| 04:17 | | 04:17 | ||
− | | # If the value entered is equal to zero | + | | # If the value entered is equal to zero then it prints a message |
|- | |- | ||
| 04:24 | | 04:24 | ||
− | | # Else it goes to the function '''factorial''' | + | | # Else it goes to the function '''factorial'''. |
|- | |- | ||
| 04:29 | | 04:29 | ||
− | | # Here, if the value is equal to one | + | | # Here, if the value is 'equal to one' then it prints value as 'one'. |
|- | |- | ||
| 04:36 | | 04:36 | ||
− | | # If not, it makes a '''recursive call''' until the value is equal to one | + | | # If not, it makes a '''recursive call''' until the value is equal to one. |
|- | |- | ||
|04:44 | |04:44 | ||
− | | # Then, all the values are multiplied and displayed | + | | # Then, all the values are multiplied and displayed. |
|- | |- | ||
Line 265: | Line 265: | ||
|- | |- | ||
| 04:52 | | 04:52 | ||
− | | Press the ''' | + | | Press the '''up-arrow''' key. |
|- | |- | ||
Line 277: | Line 277: | ||
|- | |- | ||
| 05:00 | | 05:00 | ||
− | | Now I will enter''' 5 '''as the input value. | + | | Now I will enter''' 5 '''as the '''input value'''. |
|- | |- | ||
| 05:05 | | 05:05 | ||
− | | We get the '''factorial '''of number 5 | + | | We get the '''factorial '''of number 5 |
|- | |- | ||
| 05:08 | | 05:08 | ||
− | | | + | | that is '''120.''' |
|- | |- | ||
| 05:11 | | 05:11 | ||
− | | We can see the flow of the program on '''terminal'''. | + | | We can see the flow of the program on '''terminal'''. Analyze and trace the flow of the program. |
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | Come back to our slides. | + | | Come back to our '''slides'''. |
|- | |- | ||
| 05:20 | | 05:20 | ||
− | | Let us | + | | Let us summarize. |
|- | |- | ||
| 05:21 | | 05:21 | ||
− | | In this tutorial we have | + | | In this tutorial, we have learned |
|- | |- | ||
Line 309: | Line 309: | ||
|- | |- | ||
| 05:25 | | 05:25 | ||
− | | | + | | with the help of some examples. |
|- | |- | ||
| 05:28 | | 05:28 | ||
− | | As an assignment | + | | As an assignment, |
|- | |- | ||
| 05:29 | | 05:29 | ||
− | | | + | | write a program where the '''recursive function''' calculates the sum of''' N '''numbers. |
|- | |- | ||
Line 325: | Line 325: | ||
|- | |- | ||
| 05:39 | | 05:39 | ||
− | | It | + | | It summarizes the Spoken-Tutorial project. |
|- | |- | ||
Line 333: | Line 333: | ||
|- | |- | ||
| 05:47 | | 05:47 | ||
− | | The Spoken Tutorial Project Team Conducts workshops using spoken tutorials | + | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials; |
|- | |- | ||
| 05:53 | | 05:53 | ||
− | | Gives certificates to those who pass an online test | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
Line 345: | Line 345: | ||
|- | |- | ||
| 06:06 | | 06:06 | ||
− | | Spoken Tutorial | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
Line 353: | Line 353: | ||
|- | |- | ||
| 06:18 | | 06:18 | ||
− | | More information on this | + | | More information on this mission is available at the link shown below. '''http://spoken-tutorial.org\NMEICT-Intro''' |
|- | |- | ||
Line 361: | Line 361: | ||
|- | |- | ||
| 06:29 | | 06:29 | ||
− | | This is Ashwini Patil signing off. | + | | This is Ashwini Patil. signing off. |
|- | |- |
Revision as of 20:09, 6 July 2015
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 up-arrow 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 up-arrow 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. Analyze and trace the flow of the program. |
05:18 | Come back to our slides. |
05:20 | Let us summarize. |
05:21 | In this tutorial, we have learned |
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 summarizes 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. |