~ubuntu-branches/ubuntu/warty/bash/warty

« back to all changes in this revision

Viewing changes to examples/functions/fact

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2002-04-08 20:51:41 UTC
  • Revision ID: james.westby@ubuntu.com-20020408205141-qovkhu3a90201hf2
Tags: upstream-2.05a
ImportĀ upstreamĀ versionĀ 2.05a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Who said shells can't use recursion?  Here is a factorial function.
 
2
# You call it with a number as an argument, and it returns the factorial
 
3
# of that number.
 
4
 
 
5
fact ()
 
6
{
 
7
    local num=$1;
 
8
    if [ "$num" = 1 ] ; then
 
9
        echo 1
 
10
        return ;
 
11
    fi;
 
12
    echo $(( $num * $(fact $(( $num - 1 )) ) ))
 
13
}