BASH/C3/More-on-functions/English

From Script | Spoken-Tutorial
Revision as of 22:28, 20 March 2014 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: More on functions

Author: Lavitha Pereira

Keywords: Video tutorial, Bash shell, function, function call


Visual Cue
Narration
Display Slide 1 Dear friends, Welcome to the spoken tutorial on More on functions
Display Slide 2 In this tutorial, we will learn
  • To pass an argument to a function
  • Define local variable within a function and
  • Define global variable in a function
  • With the help of some examples
Display Slide 3Prerequisites


To follow this tutorial you should have knowledge of Shell Scripting in BASH.

If not, for relevant tutorials please visit our website which is as shown,(http://www.spoken-tutorial.org)

Display Slide 4

System requirements

For this tutorial I am using
  • Ubuntu Linux 12.04 Operating System and
  • GNU BASH version 4.2

Please note, GNU Bash version 4 or above is recommended to practice this tutorial.

Let us first learn how to pass an argument to a function, and its usage.
Let me open a file 'function_parameters.sh'
Highlight #!/usr/bin/env bash This is the shebang line.
Highlight say_welcome()

{

Our function name is say_welcome


Open curly bracket opens the function definition.

Highlight echo "Welcome to $1 $2" $1 is the first positional parameter

$2 is the second postional parameter

Highlight } Close curly bracket closes the function definition.
Highlight

say_welcome "Bash" "learning"

Here, the function 'say_welcome' is called with arguments.


The syntax is, function name i.e. say welcome


...followed by the arguments within double quotes, i.e. Bash and learning.

Highlight

say_welcome "functions in" "Bash"


In a similar manner, I will call the same function with a different set of arguments.


So, I have

say_welcome space within double quote functions in space and within double quote Bash.

Save and Switch to terminal Save the file and go to the terminal.
Type

chmod +x function_parameters.sh>>Press Enter


Type:chmod space plus x space function underscore parameters dot sh


Press Enter.

Type

./function_parameters.sh>>Press Enter

Type dot slash function underscore parameters dot sh

Press Enter.

Terminal

[Output]

Welcome to Bash learning

Welcome to functions in Bash


We see that the positional parameters were substituted by the arguments passed to the function.


Dollar 1($1) was substituted by the string Bash and Dollar 2($2) with learning.


Then again, Dollar 1($1) was substituted by functions in and Dollar 2($2) with Bash.

Display slide 5

Local and Global variables in functions

* In Bash, variables can be declared as local variables and global variables.
  • Local variable:
  • It's value will be valid within the function in which it is defined.
  • Local variables are declared using keyword 'local'.
Display slide 6

Local and Global variables in functions

Global variable:
  • The value of a global variable can be accessed throughout a Bash script.
Let us learn these 2 ways to declare a variable within a function.
Let me open a file 'function_local.sh'
Highlight #!/usr/bin/env bash This is the shebang line.
say_hello(){ Function name is say underscore hello
Highlight local first_name=$1

#Local variable

Here, variable first_name is declared with keyword local.


Which means, its value will be valid within the function say_hello only.

Highlight last_name=$2

# Global variable

A variable declared without any keyword, is treated as a global variable.


So, the variable last_name can be accessed throughout the script.

Highlight echo "Hello $first_name $middle_name $last_name"

Highlight }

In this echo line, we will display the value of variables
  • first_name,
  • middle_name and
  • last_name

After this, we close this function.

Highlight middle_name="K"

#global variable

Now, here the variable middle_name is declared without keyword.


So, its value will be global throughout the script.

Highlight say_hello "Pratik" "Patil" Once again, we will call the function here.


We pass two arguments to this function call, namely, “Pratik” and “Patil”.

Highlight

echo "My first name is: $first_name"

echo "My middle name is: $middle_name"

echo "My last name is: $last_name"

These echo statements will display the value of variables
  • $first_name,
  • $middle_name and
  • $last_name
Please keep in mind that variable first_name is a local variable.
Save and Switch to terminal Save the file and go to the terminal.
Type

chmod +x function_local.sh>>Press Enter

Type:chmod space plus x space function underscore local dot sh


Press Enter.

Type ./function_local.sh>> Press Enter Now type

dot slash function underscore local dot sh


Press Enter.

Terminal

[Output]

Hello Pratik K Patil

The first line of output displays the message Hello Pratik K Patil.

Here, the variable first_name that contains value “Pratik” is local.


Which means the value is limited to the function.

Now, let us see how the local variable behaves outside the function.
Terminal

[Output]

My first name is:

My middle name is: k

My last name is: Patil

Here, nothing is displayed in first_name.


This is because the value of first_name is local to the function.


And it is not available outside the function.


middle_name and last_name are printed as they are global variables.

Hope the difference is clear to you.


Let us now summarise.

Display Slide 7

Summary

In this tutorial, we learnt
  • To pass arguments to a function
  • To declare Local variable in a function
  • To declare Global variable in a function
  • with the help of a few examples
Display Slide 7

Assignment

As an assignment.

Write a program,

  • Where the function accepts two arguments.
  • The function should multiply the two arguments.
  • Make 3 function calls with arguments (1, 2), (2, 3) and (3, 4)


Display Slide 8

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

About the Spoken Tutorial Project

Watch the video available at the link shown below.


It summarises the Spoken Tutorial project.


If you do not have good bandwidth, you can download and watch it.

Display Slide 9

Spoken Tutorial Workshops

The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide 10

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project.


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

Display Slide 11 The script has been contributed by FOSSEE and Spoken-Tutorial teams.


This is Ashwini Patil from IIT Bombay signing off.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey