Difference between revisions of "BASH/C3/More-on-functions/English-timed"
From Script | Spoken-Tutorial
(Created page with " {|border=1 |'''Time''' |'''Narration''' |- | 00:01 |Welcome to the spoken tutorial on '''More on functions ''' |- | 00:06 | In this tutorial, we will learn |- |00:09 |To...") |
|||
(6 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | |Welcome to the spoken tutorial on '''More on functions ''' | + | |Welcome to the '''spoken tutorial''' on '''More on functions.''' |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | In this tutorial, we will learn | + | | In this tutorial, we will learn: |
|- | |- | ||
|00:09 | |00:09 | ||
− | |To pass an '''argument''' to a '''function ''' | + | | To pass an '''argument''' to a '''function'''; |
|- | |- | ||
|00:11 | |00:11 | ||
− | | | + | | To define '''local variable''' within a '''function''' and |
|- | |- | ||
| 00:16 | | 00:16 | ||
− | | | + | | To define '''global variable''' in a '''function''' |
|- | |- | ||
| 00:19 | | 00:19 | ||
− | | | + | |with the help of some examples. |
|- | |- | ||
| 00:23 | | 00:23 | ||
− | | To follow this tutorial you should have knowledge of '''Shell Scripting''' in''' BASH.''' | + | | To follow this tutorial, you should have knowledge of '''Shell Scripting''' in''' BASH.''' |
|- | |- | ||
| 00:28 | | 00:28 | ||
− | |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:35 | | 00:35 | ||
− | | For this tutorial I am using | + | | For this tutorial, I am using: |
|- | |- | ||
|00:37 | |00:37 | ||
− | |'''Ubuntu Linux''' 12.04''' ''' | + | | '''Ubuntu Linux''' 12.04 '''Operating System''' |
|- | |- | ||
|00:42 | |00:42 | ||
− | |'''GNU BASH''' version 4.2 | + | | '''GNU BASH''' version 4.2. |
|- | |- | ||
| 00:45 | | 00:45 | ||
− | |Please note,''' GNU Bash | + | |Please note,''' GNU Bash''' version 4 or above is recommended to practice this tutorial. |
|- | |- | ||
| 00:52 | | 00:52 | ||
− | | Let us first learn how to pass an '''argument''' to a '''function | + | | Let us first learn how to pass an '''argument''' to a '''function''' and its usage. |
|- | |- | ||
| 00:59 | | 00:59 | ||
− | | Let me open a file ''''function_(underscore) parameters.sh'''' | + | | Let me open a file''' 'function_(underscore) parameters.sh' '''. |
|- | |- | ||
Line 66: | Line 66: | ||
|- | |- | ||
|01:08 | |01:08 | ||
− | | Our '''function | + | | Our '''function''' name is '''say_(underscore)welcome'''. |
|- | |- | ||
| 01:13 | | 01:13 | ||
− | |Open curly bracket opens the '''function | + | |Open curly bracket opens the '''function''' definition. |
|- | |- | ||
|01:18 | |01:18 | ||
− | | ''' | + | | '$1' '''(Dollar one)''' is the first '''positional parameter'''. |
|- | |- | ||
| 01:22 | | 01:22 | ||
− | |''' | + | |'$2' '''(Dollar two)''' is the second '''positional parameter'''. |
|- | |- | ||
|01:26 | |01:26 | ||
− | | Close curly bracket closes the''' function definition | + | | Close curly bracket closes the''' function definition'''. |
|- | |- | ||
|01:30 | |01:30 | ||
− | | Here, the function ''''say_welcome'''' is called with '''arguments'''. | + | | Here, the function''' 'say_welcome' '''is called with '''arguments'''. |
|- | |- | ||
|01:35 | |01:35 | ||
− | |The syntax is | + | |The syntax is- '''function''' name i.e. "say_welcome" followed by the '''arguments '''within double quotes, i.e. "Bash" and "learning". |
|- | |- | ||
|01:49 | |01:49 | ||
− | | In a similar manner, I will call the same '''function''' with a different set of '''arguments | + | | In a similar manner, I will '''call''' the same '''function''' with a different set of '''arguments'''. So, I have''' say_welcome''' space within double quotes '''functions in''' space within double quotes '''Bash'''. |
|- | |- | ||
| 02:05 | | 02:05 | ||
− | | Save the file and go to the terminal. | + | | '''Save''' the file and go to the '''terminal'''. |
|- | |- | ||
| 02:08 | | 02:08 | ||
− | | Type '''chmod space plus x space function underscore parameters dot sh''' | + | | Type: '''chmod space plus x space function underscore parameters dot sh'''. |
|- | |- | ||
Line 110: | Line 110: | ||
|- | |- | ||
| 02:19 | | 02:19 | ||
− | | Type '''dot slash function underscore parameters dot sh''' | + | | Type: '''dot slash function underscore parameters dot sh'''. |
|- | |- | ||
Line 118: | Line 118: | ||
|- | |- | ||
|02:28 | |02:28 | ||
− | | We see that the | + | | We see that the '''positional parameters''' were substituted by the '''arguments''' passed to a '''function'''. |
|- | |- | ||
|02:36 | |02:36 | ||
− | | | + | | Dollar 1 ($1) was substituted by the '''string''' "Bash" and Dollar 2 ($2) with "learning". |
|- | |- | ||
|02:45 | |02:45 | ||
− | | Then again, | + | | Then again, Dollar 1 ($1) was substituted by "functions in" and Dollar 2 ($2) with "Bash". |
|- | |- | ||
Line 138: | Line 138: | ||
|- | |- | ||
|03:03 | |03:03 | ||
− | | | + | |Its value will be valid within the '''function''' in which it is defined. |
|- | |- | ||
|03:10 | |03:10 | ||
− | | '''Local variables''' are declared using keyword '''local''' | + | | '''Local variables''' are declared using keyword '''local'''. |
|- | |- | ||
|03:15 | |03:15 | ||
− | | '''Global variable''' | + | | '''Global variable''': |
|- | |- | ||
|03:17 | |03:17 | ||
− | | The value of a '''global variable''' can be accessed throughout a '''Bash ''' | + | | The value of a '''global variable''' can be accessed throughout a '''Bash script'''. |
|- | |- | ||
Line 158: | Line 158: | ||
|- | |- | ||
| 03:29 | | 03:29 | ||
− | | Let me open a file named '''function_( | + | | Let me open a file named''' 'function_(underscore)local.sh''''. |
|- | |- | ||
Line 166: | Line 166: | ||
|- | |- | ||
| 03:39 | | 03:39 | ||
− | | Function name is '''say_(underscore) hello''' | + | | '''Function''' name is '''say_(underscore) hello'''. |
|- | |- | ||
|03:43 | |03:43 | ||
− | | Here, '''variable''' '''first_name '''is declared with '''keyword''' '''local | + | | Here, '''variable''' '''first_name '''is declared with '''keyword''' '''local''' |
|- | |- | ||
|03:49 | |03:49 | ||
− | | | + | |which means its value will be valid within the function '''say_hello '''only. |
|- | |- | ||
| 03:55 | | 03:55 | ||
− | | A '''variable '''declared without any keyword | + | | A '''variable '''declared without any keyword is treated as a '''global variable'''. |
|- | |- | ||
|04:01 | |04:01 | ||
− | |So, the ''' | + | |So, the variable '''last_name '''can be accessed throughout the '''script'''. |
|- | |- | ||
Line 194: | Line 194: | ||
|- | |- | ||
|04:14 | |04:14 | ||
− | |'''middle_name ''' | + | |'''middle_name ''' and '''last_name'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 206: | Line 202: | ||
|- | |- | ||
| 04:21 | | 04:21 | ||
− | | Now, here the variable '''middle_name ''' is declared without keyword. So, its value will be '''global ''' throughout the script. | + | | Now, here, the variable '''middle_name ''' is declared without keyword. So, its value will be '''global ''' throughout the script. |
|- | |- | ||
| 04:30 | | 04:30 | ||
− | | Once again, we | + | | Once again, we '''call''' the '''function '''here. |
|- | |- | ||
| 04:34 | | 04:34 | ||
− | | We pass two '''arguments''' to this''' function call''' | + | | We pass two '''arguments''' to this''' function call''' namely- “Pratik” and “Patil”. |
|- | |- | ||
| 04:41 | | 04:41 | ||
− | | These '''echo | + | | These '''echo''' statements will display the value of '''variables'''- |
|- | |- | ||
|04:45 | |04:45 | ||
− | | '''$first_name, ''' | + | | '''$first_name, ''''''$middle_name '''and '''$last_name'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | Please keep in mind that ''' | + | | Please keep in mind that variable '''first_name '''is a '''local variable.''' |
|- | |- | ||
| 04:57 | | 04:57 | ||
− | | Save the file and go to the '''terminal.''' | + | | '''Save''' the file and go to the '''terminal.''' |
|- | |- | ||
| 05:00 | | 05:00 | ||
− | | Type '''chmod space plus x space function underscore local dot sh''' | + | | Type: '''chmod space plus x space function underscore local dot sh'''. |
|- | |- | ||
Line 246: | Line 238: | ||
|- | |- | ||
| 05:11 | | 05:11 | ||
− | | Type '''dot slash function underscore local dot sh''' | + | | Type: '''dot slash function underscore local dot sh''' |
|- | |- | ||
Line 253: | Line 245: | ||
|- | |- | ||
− | |||
|05:18 | |05:18 | ||
− | | The first line of output displays the message '''Hello Pratik K Patil.''' | + | | The first line of output displays the message '''"Hello Pratik K Patil".''' |
|- | |- | ||
|05:25 | |05:25 | ||
− | | Here, the variable '''first_name '''that contains value | + | | Here, the variable '''first_name '''that contains value "Pratik" is '''local''' |
|- | |- | ||
|05:31 | |05:31 | ||
− | | | + | | which means the value is limited to the '''function'''. |
|- | |- | ||
Line 279: | Line 270: | ||
|- | |- | ||
|05:53 | |05:53 | ||
− | | '''middle_name '''and''' last_name '''are printed as they are '''global | + | | '''middle_name '''and''' last_name '''are printed as they are '''global variables.''' |
|- | |- | ||
Line 287: | Line 278: | ||
|- | |- | ||
|06:02 | |06:02 | ||
− | |Let us now | + | |Let us now summarize. |
|- | |- | ||
| 06:04 | | 06:04 | ||
− | | In this tutorial, we | + | | In this tutorial, we learned: |
|- | |- | ||
|06:07 | |06:07 | ||
− | |To pass '''arguments''' to a''' function ''' | + | |To pass '''arguments''' to a''' function'''; |
+ | To declare '''local variable '''in a '''function'''; | ||
|- | |- | ||
|06:14 | |06:14 | ||
− | |To declare ''' | + | |To declare '''global variable '''in a '''function''' with the help of a few examples. |
|- | |- | ||
|06:20 | |06:20 | ||
− | | As an assignment | + | | As an assignment- |
|- | |- | ||
|06:22 | |06:22 | ||
− | | | + | |write a program where the '''function''' accepts two '''arguments'''. The '''function''' should multiply the two '''arguments'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
|06:31 | |06:31 | ||
− | | Make 3 '''function calls''' with '''arguments''' (1, 2), (2, 3) and (3, 4) | + | | Make 3 '''function calls''' with '''arguments''' (1, 2), (2, 3) and (3, 4). |
|- | |- | ||
Line 323: | Line 311: | ||
|- | |- | ||
| 06:43 | | 06:43 | ||
− | | It | + | | It summarizes the Spoken-Tutorial project. If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 06:51 | | 06:51 | ||
− | | The Spoken Tutorial Project | + | | The Spoken Tutorial Project team: |
+ | Conducts workshops using spoken tutorials; | ||
+ | Gives certificates to those who pass an online test. | ||
|- | |- | ||
Line 335: | Line 325: | ||
|- | |- | ||
| 07:07 | | 07:07 | ||
− | | Spoken Tutorial | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
|07:11 | |07:11 | ||
− | |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: http://spoken-tutorial.org/NMEICT-Intro |
|- | |- | ||
Line 347: | Line 337: | ||
|- | |- | ||
|07:31 | |07:31 | ||
− | |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:09, 23 March 2017
Time | Narration |
00:01 | Welcome to the spoken tutorial on More on functions. |
00:06 | In this tutorial, we will learn: |
00:09 | To pass an argument to a function; |
00:11 | To define local variable within a function and |
00:16 | To define global variable in a function |
00:19 | with the help of some examples. |
00:23 | To follow this tutorial, you should have knowledge of Shell Scripting in BASH. |
00:28 | If not, for relevant tutorials, please visit our website which is as shown: (http://www.spoken-tutorial.org) |
00:35 | For this tutorial, I am using: |
00:37 | Ubuntu Linux 12.04 Operating System |
00:42 | GNU BASH version 4.2. |
00:45 | Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
00:52 | Let us first learn how to pass an argument to a function and its usage. |
00:59 | Let me open a file 'function_(underscore) parameters.sh' . |
01:05 | This is the shebang line. |
01:08 | Our function name is say_(underscore)welcome. |
01:13 | Open curly bracket opens the function definition. |
01:18 | '$1' (Dollar one) is the first positional parameter. |
01:22 | '$2' (Dollar two) is the second positional parameter. |
01:26 | Close curly bracket closes the function definition. |
01:30 | Here, the function 'say_welcome' is called with arguments. |
01:35 | The syntax is- function name i.e. "say_welcome" followed by the arguments within double quotes, i.e. "Bash" and "learning". |
01:49 | In a similar manner, I will call the same function with a different set of arguments. So, I have say_welcome space within double quotes functions in space within double quotes Bash. |
02:05 | Save the file and go to the terminal. |
02:08 | Type: chmod space plus x space function underscore parameters dot sh. |
02:17 | Press Enter. |
02:19 | Type: dot slash function underscore parameters dot sh. |
02:26 | Press Enter. |
02:28 | We see that the positional parameters were substituted by the arguments passed to a function. |
02:36 | Dollar 1 ($1) was substituted by the string "Bash" and Dollar 2 ($2) with "learning". |
02:45 | Then again, Dollar 1 ($1) was substituted by "functions in" and Dollar 2 ($2) with "Bash". |
02:55 | In Bash, variables can be declared as local variables and global variables. |
03:01 | Local variable: |
03:03 | Its value will be valid within the function in which it is defined. |
03:10 | Local variables are declared using keyword local. |
03:15 | Global variable: |
03:17 | The value of a global variable can be accessed throughout a Bash script. |
03:24 | Let us learn these 2 ways to declare a variable within a function. |
03:29 | Let me open a file named 'function_(underscore)local.sh'. |
03:35 | This is the shebang line. |
03:39 | Function name is say_(underscore) hello. |
03:43 | Here, variable first_name is declared with keyword local |
03:49 | which means its value will be valid within the function say_hello only. |
03:55 | A variable declared without any keyword is treated as a global variable. |
04:01 | So, the variable last_name can be accessed throughout the script. |
04:08 | In this echo line, we will display the value of variables |
04:12 | first_name, |
04:14 | middle_name and last_name. |
04:17 | After this, we close the function. |
04:21 | Now, here, the variable middle_name is declared without keyword. So, its value will be global throughout the script. |
04:30 | Once again, we call the function here. |
04:34 | We pass two arguments to this function call namely- “Pratik” and “Patil”. |
04:41 | These echo statements will display the value of variables- |
04:45 | $first_name, '$middle_name and $last_name'. |
04:51 | Please keep in mind that variable first_name is a local variable. |
04:57 | Save the file and go to the terminal. |
05:00 | Type: chmod space plus x space function underscore local dot sh. |
05:09 | Press Enter. |
05:11 | Type: dot slash function underscore local dot sh |
05:16 | Press Enter. |
05:18 | The first line of output displays the message "Hello Pratik K Patil". |
05:25 | Here, the variable first_name that contains value "Pratik" is local |
05:31 | which means the value is limited to the function. |
05:35 | Now, let us see how the local variable behaves outside the function. |
05:41 | Here, nothing is displayed in first_name. |
05:44 | This is because the value of first_name is local to the function. And it is not available outside the function. |
05:53 | middle_name and last_name are printed as they are global variables. |
05:59 | Hope the difference is clear to you. |
06:02 | Let us now summarize. |
06:04 | In this tutorial, we learned: |
06:07 | To pass arguments to a function;
To declare local variable in a function; |
06:14 | To declare global variable in a function with the help of a few examples. |
06:20 | As an assignment- |
06:22 | write a program where the function accepts two arguments. The function should multiply the two arguments. |
06:31 | Make 3 function calls with arguments (1, 2), (2, 3) and (3, 4). |
06:39 | Watch the video available at the link shown below. |
06:43 | It summarizes the Spoken-Tutorial project. If you do not have good bandwidth, you can download and watch it. |
06:51 | The Spoken Tutorial Project team:
Conducts workshops using spoken tutorials; Gives certificates to those who pass an online test. |
07:00 | For more details, please write to contact@spoken-tutorial.org |
07:07 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
07:11 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this mission is available at: http://spoken-tutorial.org/NMEICT-Intro |
07:26 | The script has been contributed by FOSSEE and Spoken-Tutorial teams. |
07:31 | This is Ashwini Patil from IIT Bombay, signing off. Thank you for joining. |