BASH/C3/More-on-functions/English-timed

From Script | Spoken-Tutorial
Revision as of 12:13, 6 July 2015 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search
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 $(Dollar)1 is the first positional parameter.
01:22 $(Dollar)2 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 havesay_welcome space within double quotes functions in space and 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
04:15 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 will 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,
04:46 $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
06:23 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.

Contributors and Content Editors

Pratik kamble, Sandhya.np14