BASH/C3/Arrays-and-functions/English

From Script | Spoken-Tutorial
Revision as of 08:30, 26 March 2014 by Nancyvarkey (Talk | contribs)

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

Title of script: Arrays & 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 Arrays & functions
Display Slide 2 In this tutorial, we will learn
  • To pass an array to a function
  • Use of exit statement in a function
  • Use of return statement in a function
  • With the help of some examples


Display Slide 3Prerequisites


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


You should also have knowledge of

arrays and if statement 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.

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

{


Our function name is array_display


Open curly bracket opens the function definition.

[Highlight]

array=($@)

#Takes array name as variable

The use of Dollar At-sign was explained in previous tutorials in this series.


Basically, it is used to print all arguments passed to a function.


Dollar At sign within round brackets stores array elements in variable 'array'.

echo " Array elements are: ${array[@]}"

#displays all element of an array

Dollar opening curly brace array within square bracket At sign closing curly brace


This line of code displays all the elements of an array.

echo "Second element i: ${array[1]}"

#displays the 2nd element

return 0

}

Dollar opening curly brace array within square bracket one closing curly brace


This line of code displays the second element of the array.

[Highlight]

operating_systems=(Ubuntu Fedora Redhat Suse)

operating_systems is declared with the elements Ubuntu, Fedora, Redhat and Suse
array_display ${operating_systems[@]} Here, array 'operating_systems' is passed to the function 'array_display'.
Display slide 5

Pass an array to a function

Syntax:

function_name ${array_name[@]}

The syntax to pass an array to a function is:

function_name space dollar opening curly brace array_name within square bracket 'At sign' closing curly brace

Come back to our program.
colors=(White green red blue)

array_display ${colors[@]}


Similarly, 'colors' is declared with the elements

White, green, red and blue


Here, array 'colors' is passed to the function 'array_display'.

Save and Switch to terminal Now let's save the file and go to the terminal.
Type

chmod +x function_array.sh>>Press Enter


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


Press Enter.

Type,

./function_array.sh>>Press Enter

Type dot slash function underscore array dot sh

Press Enter.

Terminal

[Output]

Array elemnts are: Ubuntu Fedora Redhat Suse

Second element is: Fedora

Array elements are: White green red blue

Second element is: green

As we can see, array elements of 'operating_systems' and 'colors' are displayed.


And the second array element of 'operating_systems' and 'colors' are also displayed.

Display slide 6

'return' and 'exit' statement in functions

In Bash, 'exit' and 'return' statements gives status code of a function or a program.
Display slide 7

Return and exit in functions

return:
  • The return statement will return to the script from where it was called.

exit:

  • exit statement will end the entire script from where it is encountered.


Let us learn these 2 ways to return within a function.
Open file return_exit.sh Let me open a file 'return_exit.sh'
Highlight

#!/usr/bin/env bash

This is the shebang line.
function return_function Function name is return_function
Highlight { Open curly brace opens the function definition.
if [ $1 == $2 ]; then This if statement compares two variables.

If the two variables are equal, then commands in 'if' are executed.

echo "This is return function"


return 0

#Returns to main program

This echo statement displays the message

This is return function.

return 0 moves the control from function to main program with status code 0(zero).

[Highlight]

echo "This will not appear"

Note that the statements after return will not be executed in a function.
fi

}

fi indicates the end of if statement
function exit_function

{

Here the function name is exit_function
if [ $1 == $2 ]; then The if statement here compares two variables.

If the two variables are equal, then the commands in 'if' are executed.

echo "This is exit function"


This echo statement displays the message "This is exit function"
exit 0 exit 0 will terminate the program.
fi

}

fi indicates the end of this if statement.
return_function 3 3 This is a function call with arguments 3 and 3.
echo "We are in main program" This displays the message "We are in main program"
exit_function 3 3 This is another function call with arguments 3 and 3.
Highlight

echo "This line is not displayed"

This echo statement displays the message "This line is not displayed"
Note that exit will terminate the program.

Anything after 'exit' will not be executed.

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

chmod +x return_exit.sh>>Press Enter

Type:chmod space plus x space return underscore exit dot sh


Press Enter.

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

dot slash return underscore exit dot sh


Press Enter.

Terminal

[Output]

This is return function

We are in main program

This is exit function

The output displays the messages as shown.

Now, let us understand the flow of the program.

Workflow of Program:

[Highlight program and explain]


  1. The control will be in main program, which is the script itself.
  2. The control goes to 'return_function' because of the function call.
  3. As the two variables are equal, it displays the message "This is return function"
  4. Then it encounters return 0. And the control flows from function to the statement below the function call, in the main program.
  5. Then it displays the message "We are in main program"
  6. After that, the control goes to 'exit_function' because of the function call.
  7. As the two variables are equal, it displays the message "This is exit function"
  8. Then it encounters exit 0. This will terminate the program.
  9. Any statement after exit will not be executed.
  10. Also, the statement:

"This line is not displayed" `

will not be executed.

Hope the difference is clear to you.

Let us now summarise.

Display Slide 8

Summary

In this tutorial, we learnt
  • To pass an array to a function
  • Use of exit in a function
  • Use of return in a function
  • With the help of some examples
Display Slide 9

Assignment

As an assignment.

Write a program,

  • Where a function adds all the elements in an array
  • The function should display the sum of elements.
  • Make 2 function calls with array elements (1, 2, 3) and (4, 5, 6)


Display Slide 10

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 11

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 12

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 13 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