Why does my recursive function not stop even if it reaches the return (0)?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I've made a recursive function which browses a list and tries to put the pieces of the list in a map. I have a function which checks if they fit on the map.



However if the map is too small the recursion should stop and return (0), but it doesn't. I put a printf("DONE") just before the return and the terminal print it but the function still running. How is this possible? DONE is printed several time even if return (0) is below



int fillit_algo (t_fill *orilst, t_map *map) 

int i;
int j;

i = 0;
j = 0;

if (orilst == NULL)
return (1);

while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);

j++;

j = 0;
i++;

printf ("DONEn");

return (0);










share|improve this question



















  • 1





    Because you call the function multiple times? What exactly is your question?

    – Osiris
    Nov 16 '18 at 11:16












  • Each recursive call is essentially a nested function call. When you reach return (0); code execution picks up in the previous call to fillit_algo and that "unwinding" continues until you return (0); from the original function call.

    – David C. Rankin
    Nov 16 '18 at 11:17











  • Using for() loops instead of while() would simplify this function.

    – joop
    Nov 16 '18 at 11:17











  • A recursion can be seen as a tree, where some branches reach the "DONE" before others do.

    – YesThatIsMyName
    Nov 16 '18 at 11:17











  • I need to stop my function when it reach the return (0). I call this function only one time. But I don't want to pick the previous call, how can I do ?

    – Stolen
    Nov 16 '18 at 11:19


















1















I've made a recursive function which browses a list and tries to put the pieces of the list in a map. I have a function which checks if they fit on the map.



However if the map is too small the recursion should stop and return (0), but it doesn't. I put a printf("DONE") just before the return and the terminal print it but the function still running. How is this possible? DONE is printed several time even if return (0) is below



int fillit_algo (t_fill *orilst, t_map *map) 

int i;
int j;

i = 0;
j = 0;

if (orilst == NULL)
return (1);

while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);

j++;

j = 0;
i++;

printf ("DONEn");

return (0);










share|improve this question



















  • 1





    Because you call the function multiple times? What exactly is your question?

    – Osiris
    Nov 16 '18 at 11:16












  • Each recursive call is essentially a nested function call. When you reach return (0); code execution picks up in the previous call to fillit_algo and that "unwinding" continues until you return (0); from the original function call.

    – David C. Rankin
    Nov 16 '18 at 11:17











  • Using for() loops instead of while() would simplify this function.

    – joop
    Nov 16 '18 at 11:17











  • A recursion can be seen as a tree, where some branches reach the "DONE" before others do.

    – YesThatIsMyName
    Nov 16 '18 at 11:17











  • I need to stop my function when it reach the return (0). I call this function only one time. But I don't want to pick the previous call, how can I do ?

    – Stolen
    Nov 16 '18 at 11:19














1












1








1








I've made a recursive function which browses a list and tries to put the pieces of the list in a map. I have a function which checks if they fit on the map.



However if the map is too small the recursion should stop and return (0), but it doesn't. I put a printf("DONE") just before the return and the terminal print it but the function still running. How is this possible? DONE is printed several time even if return (0) is below



int fillit_algo (t_fill *orilst, t_map *map) 

int i;
int j;

i = 0;
j = 0;

if (orilst == NULL)
return (1);

while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);

j++;

j = 0;
i++;

printf ("DONEn");

return (0);










share|improve this question
















I've made a recursive function which browses a list and tries to put the pieces of the list in a map. I have a function which checks if they fit on the map.



However if the map is too small the recursion should stop and return (0), but it doesn't. I put a printf("DONE") just before the return and the terminal print it but the function still running. How is this possible? DONE is printed several time even if return (0) is below



int fillit_algo (t_fill *orilst, t_map *map) 

int i;
int j;

i = 0;
j = 0;

if (orilst == NULL)
return (1);

while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);

j++;

j = 0;
i++;

printf ("DONEn");

return (0);







c dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 11:19









Flimzy

40.6k1367101




40.6k1367101










asked Nov 16 '18 at 11:12









StolenStolen

183




183







  • 1





    Because you call the function multiple times? What exactly is your question?

    – Osiris
    Nov 16 '18 at 11:16












  • Each recursive call is essentially a nested function call. When you reach return (0); code execution picks up in the previous call to fillit_algo and that "unwinding" continues until you return (0); from the original function call.

    – David C. Rankin
    Nov 16 '18 at 11:17











  • Using for() loops instead of while() would simplify this function.

    – joop
    Nov 16 '18 at 11:17











  • A recursion can be seen as a tree, where some branches reach the "DONE" before others do.

    – YesThatIsMyName
    Nov 16 '18 at 11:17











  • I need to stop my function when it reach the return (0). I call this function only one time. But I don't want to pick the previous call, how can I do ?

    – Stolen
    Nov 16 '18 at 11:19













  • 1





    Because you call the function multiple times? What exactly is your question?

    – Osiris
    Nov 16 '18 at 11:16












  • Each recursive call is essentially a nested function call. When you reach return (0); code execution picks up in the previous call to fillit_algo and that "unwinding" continues until you return (0); from the original function call.

    – David C. Rankin
    Nov 16 '18 at 11:17











  • Using for() loops instead of while() would simplify this function.

    – joop
    Nov 16 '18 at 11:17











  • A recursion can be seen as a tree, where some branches reach the "DONE" before others do.

    – YesThatIsMyName
    Nov 16 '18 at 11:17











  • I need to stop my function when it reach the return (0). I call this function only one time. But I don't want to pick the previous call, how can I do ?

    – Stolen
    Nov 16 '18 at 11:19








1




1





Because you call the function multiple times? What exactly is your question?

– Osiris
Nov 16 '18 at 11:16






Because you call the function multiple times? What exactly is your question?

– Osiris
Nov 16 '18 at 11:16














Each recursive call is essentially a nested function call. When you reach return (0); code execution picks up in the previous call to fillit_algo and that "unwinding" continues until you return (0); from the original function call.

– David C. Rankin
Nov 16 '18 at 11:17





Each recursive call is essentially a nested function call. When you reach return (0); code execution picks up in the previous call to fillit_algo and that "unwinding" continues until you return (0); from the original function call.

– David C. Rankin
Nov 16 '18 at 11:17













Using for() loops instead of while() would simplify this function.

– joop
Nov 16 '18 at 11:17





Using for() loops instead of while() would simplify this function.

– joop
Nov 16 '18 at 11:17













A recursion can be seen as a tree, where some branches reach the "DONE" before others do.

– YesThatIsMyName
Nov 16 '18 at 11:17





A recursion can be seen as a tree, where some branches reach the "DONE" before others do.

– YesThatIsMyName
Nov 16 '18 at 11:17













I need to stop my function when it reach the return (0). I call this function only one time. But I don't want to pick the previous call, how can I do ?

– Stolen
Nov 16 '18 at 11:19






I need to stop my function when it reach the return (0). I call this function only one time. But I don't want to pick the previous call, how can I do ?

– Stolen
Nov 16 '18 at 11:19













2 Answers
2






active

oldest

votes


















1














if((fillit_algo(orilst->next, map)))
return (1);


If fillit_algo return 0 after it gets called, then it is possible to print multiple "DONE" statements. So handle 'return 0' case in your code.



Example:



 if((fillit_algo(orilst->next, map)))
return (1);
else
return 0;





share|improve this answer


















  • 1





    Could be shorter written as return fillit_algo(orilst->next, map);.

    – Osiris
    Nov 16 '18 at 11:32


















1














Oh well...



It was so simple, thank you a lot I didn't mind this thing.
I've just put a "else return (0)" and it's fix !



int fillit_algo(t_fill *orilst, t_map *map)
int i;
int j;

i = 0;
j = 0;
if (orilst == NULL)
return (1);
while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);
else
return (0);

j++;

j = 0;
i++;

printf ("DONE");
return (0);





share|improve this answer


















  • 1





    Please read What should I do when someone answers my question?. In short: Don't take what the answer told you and then rewrite it in your own answer.

    – Some programmer dude
    Nov 16 '18 at 11:28











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336732%2fwhy-does-my-recursive-function-not-stop-even-if-it-reaches-the-return-0%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














if((fillit_algo(orilst->next, map)))
return (1);


If fillit_algo return 0 after it gets called, then it is possible to print multiple "DONE" statements. So handle 'return 0' case in your code.



Example:



 if((fillit_algo(orilst->next, map)))
return (1);
else
return 0;





share|improve this answer


















  • 1





    Could be shorter written as return fillit_algo(orilst->next, map);.

    – Osiris
    Nov 16 '18 at 11:32















1














if((fillit_algo(orilst->next, map)))
return (1);


If fillit_algo return 0 after it gets called, then it is possible to print multiple "DONE" statements. So handle 'return 0' case in your code.



Example:



 if((fillit_algo(orilst->next, map)))
return (1);
else
return 0;





share|improve this answer


















  • 1





    Could be shorter written as return fillit_algo(orilst->next, map);.

    – Osiris
    Nov 16 '18 at 11:32













1












1








1







if((fillit_algo(orilst->next, map)))
return (1);


If fillit_algo return 0 after it gets called, then it is possible to print multiple "DONE" statements. So handle 'return 0' case in your code.



Example:



 if((fillit_algo(orilst->next, map)))
return (1);
else
return 0;





share|improve this answer













if((fillit_algo(orilst->next, map)))
return (1);


If fillit_algo return 0 after it gets called, then it is possible to print multiple "DONE" statements. So handle 'return 0' case in your code.



Example:



 if((fillit_algo(orilst->next, map)))
return (1);
else
return 0;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 11:21









bhanu7kbhanu7k

536




536







  • 1





    Could be shorter written as return fillit_algo(orilst->next, map);.

    – Osiris
    Nov 16 '18 at 11:32












  • 1





    Could be shorter written as return fillit_algo(orilst->next, map);.

    – Osiris
    Nov 16 '18 at 11:32







1




1





Could be shorter written as return fillit_algo(orilst->next, map);.

– Osiris
Nov 16 '18 at 11:32





Could be shorter written as return fillit_algo(orilst->next, map);.

– Osiris
Nov 16 '18 at 11:32













1














Oh well...



It was so simple, thank you a lot I didn't mind this thing.
I've just put a "else return (0)" and it's fix !



int fillit_algo(t_fill *orilst, t_map *map)
int i;
int j;

i = 0;
j = 0;
if (orilst == NULL)
return (1);
while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);
else
return (0);

j++;

j = 0;
i++;

printf ("DONE");
return (0);





share|improve this answer


















  • 1





    Please read What should I do when someone answers my question?. In short: Don't take what the answer told you and then rewrite it in your own answer.

    – Some programmer dude
    Nov 16 '18 at 11:28















1














Oh well...



It was so simple, thank you a lot I didn't mind this thing.
I've just put a "else return (0)" and it's fix !



int fillit_algo(t_fill *orilst, t_map *map)
int i;
int j;

i = 0;
j = 0;
if (orilst == NULL)
return (1);
while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);
else
return (0);

j++;

j = 0;
i++;

printf ("DONE");
return (0);





share|improve this answer


















  • 1





    Please read What should I do when someone answers my question?. In short: Don't take what the answer told you and then rewrite it in your own answer.

    – Some programmer dude
    Nov 16 '18 at 11:28













1












1








1







Oh well...



It was so simple, thank you a lot I didn't mind this thing.
I've just put a "else return (0)" and it's fix !



int fillit_algo(t_fill *orilst, t_map *map)
int i;
int j;

i = 0;
j = 0;
if (orilst == NULL)
return (1);
while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);
else
return (0);

j++;

j = 0;
i++;

printf ("DONE");
return (0);





share|improve this answer













Oh well...



It was so simple, thank you a lot I didn't mind this thing.
I've just put a "else return (0)" and it's fix !



int fillit_algo(t_fill *orilst, t_map *map)
int i;
int j;

i = 0;
j = 0;
if (orilst == NULL)
return (1);
while (map->map[i] != 0)

while (map->map[i][j])

if (fill_map(map, orilst, i, j))

if((fillit_algo(orilst->next, map)))
return (1);
else
return (0);

j++;

j = 0;
i++;

printf ("DONE");
return (0);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 11:26









StolenStolen

183




183







  • 1





    Please read What should I do when someone answers my question?. In short: Don't take what the answer told you and then rewrite it in your own answer.

    – Some programmer dude
    Nov 16 '18 at 11:28












  • 1





    Please read What should I do when someone answers my question?. In short: Don't take what the answer told you and then rewrite it in your own answer.

    – Some programmer dude
    Nov 16 '18 at 11:28







1




1





Please read What should I do when someone answers my question?. In short: Don't take what the answer told you and then rewrite it in your own answer.

– Some programmer dude
Nov 16 '18 at 11:28





Please read What should I do when someone answers my question?. In short: Don't take what the answer told you and then rewrite it in your own answer.

– Some programmer dude
Nov 16 '18 at 11:28

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336732%2fwhy-does-my-recursive-function-not-stop-even-if-it-reaches-the-return-0%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線