Debian Codegraph @

ex63.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 # factorial
4 # ---------
5
6
7 # Does bash permit recursion?
8 # Well, yes, but...
9 # It's so slow that you gotta have rocks in your head to try it.
10
11
12 MAX_ARG=5
13 E_WRONG_ARGS=85
14 E_RANGE_ERR=86
15
16
17 if [scip-shell system . . `[`. -z "$1" ]
18 then
19 echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` number"
20 exit $E_WRONG_ARGS
21 fi
22
23 if [scip-shell system . . `[`. "$1" -gt $MAX_ARG ]
24 then
25 echoscip-shell system . . echo. "Out of range ($MAX_ARG is maximum)."
26 # Let's get real now.
27 # If you want greater range than this,
28 #+ rewrite it in a Real Programming Language.
29 exit $E_RANGE_ERR
30 fi
31
32 factscip-shell shell abs-guide 10-4 fact(). ()
33 {
34 local number=$1
35 # Variable "number" must be declared as local,
36 #+ otherwise this doesn't work.
37 if [scip-shell system . . `[`. "$number" -eq 0 ]
38 then
39 factorial=1 # Factorial of 0 = 1.
40 else
41 let "decrnum = number - 1"
42 factscip-shell shell abs-guide 10-4 fact(). $decrnum # Recursive function call (the function calls itself).
43 let "factorial = $number * $?"
44 fi
45
46 return $factorial
47 }
48
49 factscip-shell shell abs-guide 10-4 fact(). $1
50 echoscip-shell system . . echo. "Factorial of $1 is $?."
51
52 exit 0