Govur University Logo
--> --> --> -->
...

Describe the process of writing functions in shell scripts and provide a practical example.



In shell scripting, functions provide a way to encapsulate a group of commands into a single reusable unit. They help modularize code, improve readability, and enhance the maintainability of shell scripts. Here's the process of writing functions in shell scripts:

1. Function Declaration:

* To declare a function, use the following syntax:
```
bash`function\_name() {
# Function body
# Contains the commands and logic of the function
}`
```
2. Function Name:

* Choose a descriptive and meaningful name for your function. It should reflect the purpose or action performed by the function.
3. Function Body:

* The function body contains the commands and logic that define the behavior of the function.
* It can include any valid shell script commands, variables, loops, conditionals, and other functions.
4. Function Invocation:

* To execute a function, simply call its name followed by parentheses:
```
bash`function_name`
```
5. Function Arguments:

* Functions can accept arguments that are passed to them when they are invoked.
* Arguments can be accessed within the function using special variables like `$1`, `$2`, and so on, where `$1` represents the first argument, `$2` represents the second argument, and so on.
* Example:
```
bash`greet\_user() {
echo "Hello, $1!"
}

greet_user "John"`
```
Output: `Hello, John!`
6. Function Return:

* By default, functions in shell scripts return the exit code of the last command executed within the function.
* You can explicitly specify the return value using the `return` statement followed by an integer value.
* Example:
```
bash`calculate\_sum() {
local sum=$(( $1 + $2 ))
return $sum
}

calculate_sum 5 3
result=$?
echo "The sum is: $result"`
```
Output: `The sum is: 8`
7. Local Variables:

* It's good practice to declare variables used within a function as `local` to limit their scope to the function.
* This prevents any potential conflicts with variables outside the function.
* Example:
```
bash`calculate\_product() {
local product=$(( $1 * $2 ))
echo "The product is: $product"
}

calculate_product 4 6`
```
Output: `The product is: 24`

Here's a practical example of a shell script with a function that calculates the factorial of a given number:

```
bash`#!/bin/bash

calculate\_factorial() {
local num=$1
local factorial=1

if [ $num -lt 0 ]; then
echo "Error: Factorial of negative numbers is undefined."
return 1
fi

for (( i=1; i<=num; i++ )); do
factorial=$(( factorial * i ))
done

echo "The factorial of $num is: $factorial"
return 0
}

# Prompt user for a number
read -p "Enter a number: " input

# Call the factorial function
calculate_factorial $input

# Check the return value
if [ $? -eq 0 ]; then
echo "Factorial calculation successful."
else
echo "Factorial calculation failed."
fi`
```
This script defines the `calculate_factorial` function, which calculates the factorial of a given number. It handles negative numbers and returns appropriate error messages. The user is