
- 批處理指令碼教程
- 批處理指令碼 - 首頁
- 批處理指令碼 - 概述
- 批處理指令碼 - 環境
- 批處理指令碼 - 命令
- 批處理指令碼 - 檔案
- 批處理指令碼 - 語法
- 批處理指令碼 - 變數
- 批處理指令碼 - 註釋
- 批處理指令碼 - 字串
- 批處理指令碼 - 陣列
- 批處理指令碼 - 做決策
- 批處理指令碼 - 運算子
- 批處理指令碼 - 日期和時間
- 批處理指令碼 - 輸入/輸出
- 批處理指令碼 - 返回碼
- 批處理指令碼 - 函式
- 批處理指令碼 - 程序
- 批處理指令碼 - 別名
- 批處理指令碼 - 裝置
- 批處理指令碼 - 登錄檔
- 批處理指令碼 - 網路
- 批處理指令碼 - 列印
- 批處理指令碼 - 除錯
- 批處理指令碼 - 記錄
- 批處理指令碼資源
- 批處理指令碼 - 快速指南
- 批處理指令碼 - 有用的資源
- 批處理指令碼 - 討論
批處理指令碼 - 遞迴函式
透過讓變數更改區域性於函式並對呼叫方不可見的功能體完全封裝的能力。我們現在可以透過遞迴呼叫函式來確保每一級遞迴與它自己的一組變數一起工作,即使變數名稱正在重新使用。
以下示例展示瞭如何使用遞迴函式。
示例
示例展示瞭如何遞迴計算斐波那契數。當斐波那契演算法達到大於或等於給定輸入數字時,遞迴停止。示例首先開始使用數字 0 和 1,:myFibo 函式遞迴呼叫自身來計算下一個斐波那契數,直到它找到大於或等於 1000000000 的斐波那契數。
myFibo 函式的第一個引數是用來儲存輸出的變數名稱。此變數必須首先初始化為斐波那契數,並在呼叫函式時用作當前的斐波那契數,並在函式返回時設定為後續的斐波那契數。
@echo off set "fst = 0" set "fib = 1" set "limit = 1000000000" call:myFibo fib,%fst%,%limit% echo.The next Fibonacci number greater or equal %limit% is %fib%. echo.&pause&goto:eof :myFibo -- calculate recursively :myFibo -- calculate recursively the next Fibonacci number greater or equal to a limit SETLOCAL set /a "Number1 = %~1" set /a "Number2 = %~2" set /a "Limit = %~3" set /a "NumberN = Number1 + Number2" if /i %NumberN% LSS %Limit% call:myFibo NumberN,%Number1%,%Limit% (ENDLOCAL IF "%~1" NEQ "" SET "%~1 = %NumberN%" )goto:eof
輸出
以上命令生成以下輸出結果。
The next Fibonacci number greater or equal 1000000000 is 1134903170.
batch_script_functions.htm
廣告