how to keep variable's value in a recursive function?

조회 수: 48 (최근 30일)
Shang Gao
Shang Gao 2017년 12월 13일
답변: Walter Roberson 2017년 12월 13일
I have a recursive function that basically checks if one situation is true or false. I need to know how many times the case is true. Since if I define a variable within the function, its value will be rewritten in every recursive call. I am wondering if there is any way around it, like access to the work space variable or something.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 13일
Best approach: return the count of this function and the calls "underneath" it as a second variable.
But you could also use a global variable or a shared variable.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by