2024-09-24 22:39:42 +00:00
|
|
|
#!/usr/bin/env bellos
|
|
|
|
# File: functions.bellos
|
|
|
|
|
|
|
|
# Defining and using functions
|
|
|
|
|
2024-10-03 04:14:47 +00:00
|
|
|
# Simple function
|
2024-09-24 22:39:42 +00:00
|
|
|
function greet() {
|
2024-10-05 15:54:36 +00:00
|
|
|
echo Hello
|
2024-09-24 22:39:42 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "Testing simple function:"
|
|
|
|
greet "World"
|
|
|
|
greet "Bellos"
|
|
|
|
|
|
|
|
# Function with return value
|
2024-09-24 22:39:42 +00:00
|
|
|
function add() {
|
2024-10-03 04:14:47 +00:00
|
|
|
local result=$(($1 + $2))
|
|
|
|
echo $result
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\nTesting function with return value:"
|
|
|
|
sum=$(add 5 3)
|
|
|
|
echo "5 + 3 = $sum"
|
|
|
|
|
|
|
|
# Function with local variables
|
|
|
|
function calculate_rectangle_area() {
|
|
|
|
local length=$1
|
|
|
|
local width=$2
|
|
|
|
local area=$((length * width))
|
|
|
|
echo "The area of a rectangle with length $length and width $width is $area"
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\nTesting function with local variables:"
|
|
|
|
calculate_rectangle_area 4 5
|
|
|
|
|
|
|
|
# Recursive function (factorial)
|
|
|
|
function factorial() {
|
|
|
|
if [ $1 -le 1 ]; then
|
|
|
|
echo 1
|
|
|
|
else
|
|
|
|
local sub_fact=$(factorial $(($1 - 1)))
|
|
|
|
echo $(($1 * sub_fact))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\nTesting recursive function (factorial):"
|
|
|
|
for i in 0 1 2 3 4 5; do
|
|
|
|
result=$(factorial $i)
|
|
|
|
echo "Factorial of $i is $result"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Function with default parameter
|
|
|
|
function greet_with_default() {
|
|
|
|
local name=${1:-"Guest"}
|
|
|
|
echo "Hello, $name!"
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\nTesting function with default parameter:"
|
|
|
|
greet_with_default
|
|
|
|
greet_with_default "Alice"
|
|
|
|
|
|
|
|
# Function that modifies a global variable
|
|
|
|
global_var=10
|
|
|
|
|
|
|
|
function modify_global() {
|
|
|
|
global_var=$((global_var + 5))
|
2024-09-24 22:39:42 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "\nTesting function that modifies a global variable:"
|
|
|
|
echo "Before: global_var = $global_var"
|
|
|
|
modify_global
|
|
|
|
echo "After: global_var = $global_var"
|