Need a little help with functions

I'm semi new to unix/linux and am trying to convert a program I wrote in C++ to a bash script. It's a program that prints Fibonacci's series. I have found scripts that will do it, but I'm trying persistently to get this one to work. The problem occurs when I try to return a value from the function. I get no return or I have done something wrong within the function itself. Any help would be much appreciated. Here's what I have so far:

#!/bin/bash
#Fibonnaci_Series

fibonacci ()
{
n=$1
if [ $n -le 1 ]
then
return $n
else
l=`fibonacci $((n-1))`
r=`fibonacci $((n-2))`
return $((l+r))
fi
}

echo "Enter the limit for Fibonaaci's Series: "
read limit
for((i=0; i<=$((limit-1)); i++))
do
result=$(fibonacci $i)
echo "Fibonacci(" $((i+1)) ") = $result"
done

Use echo instead of return in your function:

#!/bin/bash

fibonacci ()
{
  n=$1
  if [ $n -le 1 ]
  then
    echo $n
  else
    l=`fibonacci $((n-1))`
    r=`fibonacci $((n-2))`
    echo  $((l+r))
  fi
}

echo "Enter the limit for Fibonaaci's Series: "

read limit
for((i=0; i<=$((limit-1)); i++))
do
  result=$(fibonacci $i)
  echo "Fibonacci(" $((i+1)) ") = $result"
done

Regards

Wow, Thanks for the reply Franklin52, that certainly does the trick. I did actually see an example similar to that but I took it to be similar to that of a void function in other languages.

I am still a bit confused here. I have used echo to output to the screen before but does it actually cause a return when used in the context of a function. The value, or maybe even a string appears to be assigned to the result variable and even outputs as I did a bit of tinkering around with it to see if I could figure it out myself.

Could somebody walk me through the first iteration of the loop here (as the first and second are the easiest). First 0 is passed as the first argument and assigned to n. As 0 is less than or equal to 1 then echo $n (in this case 0). It doesn't output at this point, but in fact appears to return and assign it to the result variable. Is this what is actually happening or could somebody clarify it a bit for me?

I have seen functions with multiple echo statements following each other, so I'm pretty sure it doesn't cause a return. This just baffles me.

To see what happens within the (recursive) function you can run your script in the debug mode:

bash -x scriptname

Replace the lines in the loop with something like this:

for((i=0; i<=$((limit-1)); i++))
do
  fibonacci $i
done