BASH/C3/Recursive-function/English
Title of script: Advance topics in function
Author: Lavitha Pereira
Keywords: Video tutorial, recursive function
|
|
Display Slide 1 | Dear friends, welcome to the spoken tutorial on Recursive function. |
Display Slide 2 | In this tutorial, we will learn
|
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
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.
|
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
|
[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,
|
Display Slide 10
Assignment |
As an assignment.
Write a program,
|
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.
|
Display Slide 12
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
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.
|
Display Slide 14 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |