Can someone explain how this recursive function works step by step?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
function outvar = recurfn(num) % Format: recurfn(number)
if num < 0
outvar = 2;
else
outvar = 4 + recurfn(num-1);
end
end
Can someone explain to me how this recursive function works step by step?
for example without using MATLAB how would i figure out the result of recurfn(1) or recurfn(2)?
Thanks!
0 commentaires
Réponse acceptée
Plus de réponses (1)
Jesus Sanchez
le 1 Déc 2019
function outvar = recurfn(num) % Format: recurfn(number)
if num < 0
outvar = 2;
else
outvar = 4 + recurfn(num-1);
end
end
As fas as I understand this, the function will call itself forever until num has a value less than 0. Thus, outvar is fixed to 2.
2 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!