函数递归

5年以前  |  阅读数:551 次  |  编程语言:Shell 
#!/bin/bash

function factorial {
    if [ $1 -eq 1 ]
    then
        echo 1
    else
        local temp=$[ $1 -1 ]
        local result=`factorial $temp`
        echo $[ $result * $1 ]
    fi
}

read -p "Please input a value: " value
result=`factorial $value`
echo "The factorial of $value is: $result"

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8