Difference between revisions of "BASH/C3/Arrays-and-functions/English-timed"
From Script | Spoken-Tutorial
(Created page with "{| border=1 |'''Time''' |'''Narration''' |- | 00:01 | Dear friends, Welcome to the spoken tutorial on '''Arrays & functions''' |- | 00:07 | In this tutorial, we will learn...") |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Dear friends, | + | | Dear friends, welcome to the spoken tutorial on '''Arrays & functions'''. |
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial, we will learn | + | | In this tutorial, we will learn: |
|- | |- | ||
|00:11 | |00:11 | ||
− | | To pass an '''array''' to a''' function ''' | + | |To pass an '''array''' to a''' function ''' |
|- | |- | ||
Line 21: | Line 21: | ||
|- | |- | ||
|00:17 | |00:17 | ||
− | | Use of '''return''' statement in a '''function''' | + | |Use of '''return''' statement in a '''function''' |
|- | |- | ||
|00:20 | |00:20 | ||
− | | | + | | with the help of some examples. |
|- | |- | ||
| 00:24 | | 00:24 | ||
− | | To follow this tutorial you should have knowledge of '''Shell Scripting.''' | + | | To follow this tutorial, you should have knowledge of '''Shell Scripting.''' |
|- | |- | ||
|00:29 | |00:29 | ||
− | |You should also have knowledge of '''arrays '''and '''if statement '''in BASH. | + | |You should also have knowledge of '''arrays '''and ''''if' statement '''in BASH. |
|- | |- | ||
|00:36 | |00:36 | ||
− | |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:43 | | 00:43 | ||
− | | For this tutorial I am using | + | | For this tutorial, I am using: |
|- | |- | ||
|00:46 | |00:46 | ||
− | | '''Ubuntu Linux''' 12.04''' '''Operating System and | + | |'''Ubuntu Linux''' 12.04''' '''Operating System and |
|- | |- | ||
|00:50 | |00:50 | ||
− | | '''GNU BASH''' version 4.2 | + | |'''GNU BASH''' version 4.2 |
|- | |- | ||
Line 57: | Line 57: | ||
|- | |- | ||
|01:02 | |01:02 | ||
− | | Let us first learn how to pass an '''array''' to a '''function | + | | Let us first learn how to pass an '''array''' to a '''function''' and its usage. |
|- | |- | ||
| 01:09 | | 01:09 | ||
− | | Let me open a file '''function_(underscore)array dot sh''' | + | | Let me open a file '''function_(underscore)array dot sh'''. |
|- | |- | ||
Line 69: | Line 69: | ||
|- | |- | ||
|01:18 | |01:18 | ||
− | | Our '''function''' name is '''array_(underscore) display''' | + | | Our '''function''' name is '''array_(underscore) display'''. |
|- | |- | ||
|01:22 | |01:22 | ||
− | |Open curly brace opens the '''function | + | |Open curly brace opens the '''function''' definition. |
|- | |- | ||
Line 81: | Line 81: | ||
|- | |- | ||
|01:34 | |01:34 | ||
− | |Basically, it is used to print all '''arguments''' passed to a '''function.''' | + | |Basically, it is used to '''print''' all '''arguments''' passed to a '''function.''' |
|- | |- | ||
|01:40 | |01:40 | ||
− | |'''Dollar @ (at sign) within round brackets | + | |'''Dollar @''' (at sign) within round brackets stores '''array elements '''in variable ''''array'.''' |
|- | |- | ||
|01:47 | |01:47 | ||
− | | '''Dollar''' opening curly brace '''array''' within square bracket @(At-sign) closing curly brace | + | | '''Dollar''' opening curly brace '''array''' within square bracket @(At-sign) closing curly brace. |
|- | |- | ||
Line 97: | Line 97: | ||
|- | |- | ||
|02:00 | |02:00 | ||
− | | '''Dollar''' opening curly brace array within square | + | | '''Dollar''' opening curly brace '''array''' within square brackets one closing curly brace. |
|- | |- | ||
|02:08 | |02:08 | ||
− | |This line of code displays the second '''element''' of the '''array''' | + | |This line of code displays the second '''element''' of the '''array'''. |
|- | |- | ||
| 02:14 | | 02:14 | ||
− | | ''' | + | | '''operating_systems '''is declared with the elements "Ubuntu", "Fedora", "Redhat" and "Suse". |
|- | |- | ||
| 02:22 | | 02:22 | ||
− | | Here, ''' | + | | Here, array '''operating_systems''' is passed to the function '''array_display.''' |
|- | |- | ||
|02:29 | |02:29 | ||
− | | The syntax to pass an '''array''' to a '''function''' is '''function_name''' space '''dollar''' opening curly brace '''array_name''' within square | + | | The syntax to pass an '''array''' to a '''function''' is '''function_name''' space '''dollar''' opening curly brace '''array_name''' within square brackets '''@(At sign)''' closing curly brace. |
|- | |- | ||
Line 121: | Line 121: | ||
|- | |- | ||
|02:48 | |02:48 | ||
− | | Similarly, '''colors''' is declared with the '''elements''' '''White, green, red '''and''' blue''' | + | | Similarly, '''colors''' is declared with the '''elements''' '''White, green, red '''and''' blue'''. |
|- | |- | ||
|02:57 | |02:57 | ||
− | |Here, '''array colors''' is passed to the '''function''' '''array_display''' | + | |Here, '''array''' '''colors''' is passed to the '''function''' '''array_display'''. |
|- | |- | ||
|03:02 | |03:02 | ||
− | | Now let's save the file and go to the '''terminal'''. | + | | Now let's '''save''' the file and go to the '''terminal'''. |
|- | |- | ||
|03:07 | |03:07 | ||
− | | Type '''chmod space plus x space function underscore array dot sh''' | + | | Type: '''chmod space plus x space function underscore array dot sh''' |
|- | |- | ||
|03:18 | |03:18 | ||
− | |Press '''Enter.''' | + | |Press '''Enter.'''Type: '''dot slash function underscore array dot sh''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 153: | Line 149: | ||
|- | |- | ||
|03:33 | |03:33 | ||
− | |And the ''' | + | |And the second '''array element''' of ''''operating_systems'''' and ''''colors'''' are also displayed. |
|- | |- | ||
|03:41 | |03:41 | ||
− | | In '''Bash''', ''''exit' '''and''' 'return' statements ''' | + | | In '''Bash''', ''' 'exit' '''and''' 'return' '''statements give '''status code''' of a '''function''' or a program. |
|- | |- | ||
| 03:49 | | 03:49 | ||
− | |The '''return''' statement will return to the script from where it was called. | + | |The '''return''' statement will return to the '''script''' from where it was called. |
|- | |- | ||
|03:54 | |03:54 | ||
− | |'''exit '''statement will end the entire script from where it is encountered. | + | |'''exit '''statement will end the entire '''script''' from where it is encountered. |
|- | |- | ||
Line 173: | Line 169: | ||
|- | |- | ||
|04:06 | |04:06 | ||
− | | Let me open a file ''''return_exit.sh'''' | + | | Let me open a file ''''return_exit.sh''''. |
|- | |- | ||
Line 181: | Line 177: | ||
|- | |- | ||
| 04:14 | | 04:14 | ||
− | | Function name is '''return_(Underscore)function ''' | + | | Function name is '''return_(Underscore)function '''. |
|- | |- | ||
| 04:18 | | 04:18 | ||
− | | Open curly brace opens the '''function | + | | Open curly brace opens the '''function''' definition. |
|- | |- | ||
|04:22 | |04:22 | ||
− | | This '''if | + | | This '''if''' statement compares two '''variables'''. |
|- | |- | ||
|04:27 | |04:27 | ||
− | |If the two variables are equal | + | |If the two variables are equal then '''commands''' in ''''if'''' are executed. |
|- | |- | ||
|04:33 | |04:33 | ||
− | | This '''echo | + | | This '''echo''' statement displays the message: |
|- | |- | ||
|04:36 | |04:36 | ||
− | |'''This is return function.''' | + | |'''"This is return function".''' |
|- | |- | ||
|04:39 | |04:39 | ||
− | | '''return 0 '''moves the control from '''function''' to main program with status code 0(zero). | + | | '''return 0 '''moves the '''control''' from '''function''' to '''main''' program with status code 0(zero). |
|- | |- | ||
Line 213: | Line 209: | ||
|- | |- | ||
| 04:54 | | 04:54 | ||
− | | '''fi''' indicates the end of '''if statement''' | + | | ''''fi'''' indicates the end of ''''if' statement'''. |
|- | |- | ||
|04:58 | |04:58 | ||
− | | Here the '''function '''name is''' exit_( | + | | Here the '''function '''name is''' exit_(Underscore)function'''. |
|- | |- | ||
|05:02 | |05:02 | ||
− | | The '''if | + | | The ''''if'''' statement here, compares two variables. |
|- | |- | ||
|05:06 | |05:06 | ||
− | | If the two variables are equal | + | | If the two variables are equal then the '''commands''' in ''''if'''' are executed. |
|- | |- | ||
| 05:14 | | 05:14 | ||
− | | This '''echo statement '''displays the message '''"This is exit function"''' | + | | This '''echo statement '''displays the message '''"This is exit function" '''. |
|- | |- | ||
Line 237: | Line 233: | ||
|- | |- | ||
| 05:23 | | 05:23 | ||
− | | '''fi''' indicates the end of this '''if statement.''' | + | | ''''fi'''' indicates the end of this ''''if' statement.''' |
|- | |- | ||
Line 245: | Line 241: | ||
|- | |- | ||
|05:33 | |05:33 | ||
− | | This displays the message '''"We are in main program"''' | + | | This displays the message '''"We are in main program"'''. |
|- | |- | ||
Line 253: | Line 249: | ||
|- | |- | ||
|05:44 | |05:44 | ||
− | | This '''echo | + | | This '''echo''' statement displays the message '''"This line is not displayed"'''. |
|- | |- | ||
Line 265: | Line 261: | ||
|- | |- | ||
|05:58 | |05:58 | ||
− | | Save the file and go to the '''terminal.''' | + | | '''Save''' the file and go to the '''terminal.''' |
|- | |- | ||
|06:00 | |06:00 | ||
− | | Type '''chmod space plus x space return underscore exit dot sh''' | + | | Type: '''chmod space plus x space return underscore exit dot sh''' |
|- | |- | ||
Line 277: | Line 273: | ||
|- | |- | ||
| 06:12 | | 06:12 | ||
− | |Type '''dot slash return underscore exit dot sh''' | + | |Type: '''dot slash return underscore exit dot sh''' |
|- | |- | ||
Line 285: | Line 281: | ||
|- | |- | ||
| 06:20 | | 06:20 | ||
− | | The output displays the messages as shown. | + | | The '''output''' displays the messages as shown. |
|- | |- | ||
Line 293: | Line 289: | ||
|- | |- | ||
| 06:27 | | 06:27 | ||
− | |The control will be in main program | + | |The '''control''' will be in '''main''' program which is the '''script''' itself. |
|- | |- | ||
Line 301: | Line 297: | ||
|- | |- | ||
| 06:39 | | 06:39 | ||
− | | As the two variables are equal, it displays the message '"This is return function''' | + | | As the two '''variables''' are equal, it displays the message '''"This is return function"'''. |
|- | |- | ||
| 06:47 | | 06:47 | ||
− | | Then it encounters '''return 0.'''And the control flows from '''function''' to the statement below the '''function call, '''in the main program. | + | | Then it encounters '''return 0.''' And the control flows from '''function''' to the '''statement''' below the '''function call,''' in the '''main program'''. |
|- | |- | ||
| 06:59 | | 06:59 | ||
− | | Then it displays the message '''We are in main program''' | + | | Then it displays the message''' "We are in main program" '''. |
|- | |- | ||
| 07:03 | | 07:03 | ||
− | | After that, the control goes to '''exit_function''' because of the '''function call'''. | + | | After that, the '''control''' goes to '''exit_function''' because of the '''function call'''. |
|- | |- | ||
| 07:11 | | 07:11 | ||
− | |As the two variables are equal, it displays the message '''"This is exit function"''' | + | |As the two variables are equal, it displays the message '''"This is exit function".''' |
|- | |- | ||
Line 329: | Line 325: | ||
|- | |- | ||
| 07:30 | | 07:30 | ||
− | | Also, the statement | + | | Also, the statement "This line is not displayed" will not be executed. |
|- | |- | ||
Line 337: | Line 333: | ||
|- | |- | ||
| 07:39 | | 07:39 | ||
− | |Let us now | + | |Let us now summarize. |
|- | |- | ||
| 07:41 | | 07:41 | ||
− | | In this tutorial, we learnt | + | | In this tutorial, we learnt: |
|- | |- | ||
Line 349: | Line 345: | ||
|- | |- | ||
| 07:47 | | 07:47 | ||
− | | Use of''' exit''' statement in a '''function''' | + | |Use of''' exit''' statement in a '''function''' |
|- | |- | ||
| 07:50 | | 07:50 | ||
− | | Use of '''return''' statement in a '''function''' | + | |Use of '''return''' statement in a '''function''' |
|- | |- | ||
|07:53 | |07:53 | ||
− | | | + | |with the help of some examples. |
|- | |- | ||
| 07:56 | | 07:56 | ||
− | | As an assignment | + | | As an assignment-Write a program |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 07:58 | | 07:58 | ||
− | | | + | | where a '''function '''adds all the '''elements''' in an '''array'''. The '''function''' should display the sum of elements. |
|- | |- | ||
| 08:07 | | 08:07 | ||
− | |Make 2 '''function''' '''calls''' with '''array elements''' (1, 2, 3) and (4, 5, 6) | + | |Make 2 '''function''' '''calls''' with '''array elements''' (1, 2, 3) and (4, 5, 6). |
|- | |- | ||
|08:15 | |08:15 | ||
− | | Watch the video available at the link shown below. http://spoken-tutorial.org / | + | | Watch the video available at the link shown below. http://spoken-tutorial.org/What_is_a_Spoken_Tutorial |
|- | |- | ||
| 08:19 | | 08:19 | ||
− | |It | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
Line 389: | Line 381: | ||
|- | |- | ||
|08:28 | |08:28 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: |
|- | |- | ||
| 08:30 | | 08:30 | ||
− | | Conducts workshops using spoken tutorials | + | | Conducts workshops using spoken tutorials.Gives certificates to those who pass an online test.For more details, please write to contact@spoken-tutorial.org |
|- | |- | ||
|08:45 | |08:45 | ||
− | | Spoken Tutorial Project is a part of the Talk to a Teacher project. | + | | '''Spoken Tutorial''' Project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
| 08:49 | | 08:49 | ||
− | |It is supported by the National Mission on Education through ICT, MHRD, Government of India.More information on this | + | |It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this mission is available at the link shown below. http://spoken-tutorial.org/NMEICT-Intro |
|- | |- | ||
Line 409: | Line 401: | ||
|- | |- | ||
| 09:10 | | 09:10 | ||
− | |This is Ashwini Patil from IIT Bombay signing off. Thank you for joining. | + | |This is Ashwini Patil from IIT Bombay, signing off. Thank you for joining. |
|} | |} |
Latest revision as of 17:12, 23 March 2017
Time | Narration |
00:01 | Dear friends, welcome to the spoken tutorial on Arrays & functions. |
00:07 | In this tutorial, we will learn: |
00:11 | To pass an array to a function |
00:14 | Use of exit statement in a function |
00:17 | Use of return statement in a function |
00:20 | with the help of some examples. |
00:24 | To follow this tutorial, you should have knowledge of Shell Scripting. |
00:29 | You should also have knowledge of arrays and 'if' statement in BASH. |
00:36 | If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org |
00:43 | For this tutorial, I am using: |
00:46 | Ubuntu Linux 12.04 Operating System and |
00:50 | GNU BASH version 4.2 |
00:54 | Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
01:02 | Let us first learn how to pass an array to a function and its usage. |
01:09 | Let me open a file function_(underscore)array dot sh. |
01:15 | This is the shebang line. |
01:18 | Our function name is array_(underscore) display. |
01:22 | Open curly brace opens the function definition. |
01:27 | The use of Dollar @(at-sign) was explained in previous tutorials in this series. |
01:34 | Basically, it is used to print all arguments passed to a function. |
01:40 | Dollar @ (at sign) within round brackets stores array elements in variable 'array'. |
01:47 | Dollar opening curly brace array within square bracket @(At-sign) closing curly brace. |
01:55 | This line of code displays all the elements of an array. |
02:00 | Dollar opening curly brace array within square brackets one closing curly brace. |
02:08 | This line of code displays the second element of the array. |
02:14 | operating_systems is declared with the elements "Ubuntu", "Fedora", "Redhat" and "Suse". |
02:22 | Here, array operating_systems is passed to the function array_display. |
02:29 | The syntax to pass an array to a function is function_name space dollar opening curly brace array_name within square brackets @(At sign) closing curly brace. |
02:45 | Come back to our program. |
02:48 | Similarly, colors is declared with the elements White, green, red and blue. |
02:57 | Here, array colors is passed to the function array_display. |
03:02 | Now let's save the file and go to the terminal. |
03:07 | Type: chmod space plus x space function underscore array dot sh |
03:18 | Press Enter.Type: dot slash function underscore array dot sh |
03:25 | Press Enter. |
03:27 | As we can see, array elements of 'operating_systems' and 'colors' are displayed. |
03:33 | And the second array element of 'operating_systems' and 'colors' are also displayed. |
03:41 | In Bash, 'exit' and 'return' statements give status code of a function or a program. |
03:49 | The return statement will return to the script from where it was called. |
03:54 | exit statement will end the entire script from where it is encountered. |
04:01 | Let us learn these 2 ways to return within a function. |
04:06 | Let me open a file 'return_exit.sh'. |
04:12 | This is the shebang line. |
04:14 | Function name is return_(Underscore)function . |
04:18 | Open curly brace opens the function definition. |
04:22 | This if statement compares two variables. |
04:27 | If the two variables are equal then commands in 'if' are executed. |
04:33 | This echo statement displays the message: |
04:36 | "This is return function". |
04:39 | return 0 moves the control from function to main program with status code 0(zero). |
04:47 | Note that the statements after return will not be executed in a function. |
04:54 | 'fi' indicates the end of 'if' statement. |
04:58 | Here the function name is exit_(Underscore)function. |
05:02 | The 'if' statement here, compares two variables. |
05:06 | If the two variables are equal then the commands in 'if' are executed. |
05:14 | This echo statement displays the message "This is exit function" . |
05:19 | exit 0 will terminate the program. |
05:23 | 'fi' indicates the end of this 'if' statement. |
05:27 | This is a function call with arguments 3 and 3. |
05:33 | This displays the message "We are in main program". |
05:38 | This is another function call with arguments 3 and 3. |
05:44 | This echo statement displays the message "This line is not displayed". |
05:49 | Note that exit will terminate the program. |
05:53 | Anything after exit will not be executed. |
05:58 | Save the file and go to the terminal. |
06:00 | Type: chmod space plus x space return underscore exit dot sh |
06:09 | Press Enter. |
06:12 | Type: dot slash return underscore exit dot sh |
06:18 | Press Enter. |
06:20 | The output displays the messages as shown. |
06:24 | Now, let us understand the flow of the program. |
06:27 | The control will be in main program which is the script itself. |
06:33 | The control goes to return_function because of the function call. |
06:39 | As the two variables are equal, it displays the message "This is return function". |
06:47 | Then it encounters return 0. And the control flows from function to the statement below the function call, in the main program. |
06:59 | Then it displays the message "We are in main program" . |
07:03 | After that, the control goes to exit_function because of the function call. |
07:11 | As the two variables are equal, it displays the message "This is exit function". |
07:19 | Then it encounters exit 0. This will terminate the program. |
07:25 | Any statement after exit will not be executed. |
07:30 | Also, the statement "This line is not displayed" will not be executed. |
07:36 | Hope the difference is clear to you. |
07:39 | Let us now summarize. |
07:41 | In this tutorial, we learnt: |
07:44 | To pass an array to a function |
07:47 | Use of exit statement in a function |
07:50 | Use of return statement in a function |
07:53 | with the help of some examples. |
07:56 | As an assignment-Write a program |
07:58 | where a function adds all the elements in an array. The function should display the sum of elements. |
08:07 | Make 2 function calls with array elements (1, 2, 3) and (4, 5, 6). |
08:15 | Watch the video available at the link shown below. http://spoken-tutorial.org/What_is_a_Spoken_Tutorial |
08:19 | It summarizes the Spoken Tutorial project. |
08:23 | If you do not have good bandwidth, you can download and watch it. |
08:28 | The Spoken Tutorial Project Team: |
08:30 | Conducts workshops using spoken tutorials.Gives certificates to those who pass an online test.For more details, please write to contact@spoken-tutorial.org |
08:45 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
08:49 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this mission is available at the link shown below. http://spoken-tutorial.org/NMEICT-Intro |
09:04 | The script has been contributed by FOSSEE and Spoken-Tutorial teams. |
09:10 | This is Ashwini Patil from IIT Bombay, signing off. Thank you for joining. |