BASH/C3/Recursive-function/English

From Script | Spoken-Tutorial
Revision as of 11:57, 6 June 2014 by Ashwini (Talk | contribs)

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

Title of script: Advance topics in function

Author: Lavitha Pereira

Keywords: Video tutorial, recursive function


Visual Cue
Narration
Display Slide 1 Dear friends, welcome to the spoken tutorial on Recursive function.
Display Slide 2 In this tutorial, we will learn
  • What is a Recursive 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.

Display slide 8

Recursion


Let us see what a recursive function is.
  • A recursive function is one which calls itself
  • Recursion is a useful technique for simplifying complex algorithms


Let me open a file named factorial.sh
Highlight

#!/usr/bin/env bash

I have typed some code in this file.

This is the shebang line.

factorial()

{

}

factorial is the function name.

and it is an empty function.



factorial() {

echo "We are inside a factorial function"

}

Let's write a line of code that will print a message “We are inside a factorial function”



#main script

read -p "Enter the number:" n

This statement reads user's input and stores the value in variable 'n'



if [ $n -eq 0 ]; then

echo "factorial value of $n is 1"

Here I have an if-else condition.

If condition checks whether the value of 'n' is equal to zero.

If true, it will display the message "factorial value of 0 is 1".

else

#calling factorial function

factorial $n

fi

Here is the else part of the if statement.

It calls the factorial function.

And fi is the end of the if-else statement.

Switch to terminal Let us run the file factorial.sh.

Open the terminal using CTRL+ALT+T keys simultaneously on your keyboard.

Type

chmod +x factorial.sh>>Press Enter

dot slash factorial.sh

Type:chmod space plus x space factorial dot sh

Press Enter.

Type dot slash factorial.sh

Press Enter.

Type:0 I will enter 0 and press Enter.
Highlight

Output

We see the output as:

factorial value of 0 is 1

dot slash factorial.sh

Type:

5

Now press the uparrow key.

Recall the previous command.

Press Enter.

This time, I will enter 5 and press Enter.

Highlight

Output

The output is:

We are inside a factorial function.

Now let us add some more logic to the factorial function.

We will calculate the factorial of a number.

Come back to our code.

Type:

temp=$1

if [ $temp -eq 1 ]; then

echo "1"

else

f=$((temp-1))

#Recursive call

f=$(factorial $f)

f=$((f*temp))

echo $f

fi

Now let us replace, the echo statement inside the factorial function with the code block.

Click on Save



temp=$1 temp is a variable and stores the value entered by user.
if [ $temp -eq 1 ]; then

echo “1”

If condition checks whether the variable value is equal to 1.

If true, it will prints 1.

else

f=$((temp-1))

This is the else part of the if statement.

This reduces one from the temp variable value.

And stores the result in a variable 'f'.

#Recursive call

f=$(factorial $f)

Variable f stores the output of factorial function.

This is a recursive call.

f=$((f*temp))

echo $f

Value of variable f and temp is multiplied and stored in f.

Then we print the vaue of f.

fi

}

End of if-else statement and function.
Save the file and come to the slides.

Let us understand the flow of the program.

Workflow of Program

<<Flowchart>>

# The value of n is taken from the user ie n
  1. If the value entered is equal to zero then it prints a message
  2. Else it goes to the function factorial
  3. Here, if the value is equal to one then it prints value as one
  4. If not, it makes a recursive call until the value is equal to one
  5. Then, all the values are multiplied and displayed


[Highlight]

./factorial.sh

Press Enter

Now come back to the terminal.

Press the uparrow key.

Recall the previous command.

./factorial.sh

Press Enter.

Now I will enter 5 as the input value.

Output We get the factorial of number 5.

ie 120.

Terminal

[Output]


We can see the flow of the program on terminal.

Analyse and trace the flow of program.

Come back to our slides.

Display Slide 10

Summary

Let us summarise.

In this tutorial we have learnt,

  • Recursive function
  • With the help of some examples


Display Slide 10

Assignment

As an assignment.

Write a program,

  • where the recursive function calculates the sum of N numbers


Display Slide 11

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 12

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 13

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 14 The script has been contributed by FOSSEE and Spoken-Tutorial teams.


This is Ashwini from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey