return to first call in recursive call

3 ビュー (過去 30 日間)
Sidafa
Sidafa 2015 年 8 月 25 日
回答済み: Walter Roberson 2015 年 8 月 25 日
Hello,
Is there a way to return to the head of the stack of recursive function calls? or exit the recursive calls?
I have a driver file (driver_main.m) which calls functionA. But functionA recursively calls itself until a condition is met. Then I want to return to driver_main.m
Right now, when this condition is met, the stack is rather long. and when the return is executed, it returns back to functionA and not driver_main.m
Is there a way to force return to driver_main.m even when the stack is long? When I call dbstack, driver_main.m is at the bottom of the list.
  1 件のコメント
Geoff Hayes
Geoff Hayes 2015 年 8 月 25 日
Sidafa - can you provide a short sample of your recursive function? Ideally, it should be written so that it always unwinds as it "makes its way" back to the calling function.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 25 日
The only way, and it is not recommended, would be to use try/catch and have the recursive routine throw an error.
Recursive routines should always be written for smooth return. If you need to return an "I found it" flag that tells the calling routine to return, then do that.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by