G+_Jason Perry Posted November 15, 2015 Share Posted November 15, 2015 Question: is it okay to nest a call to a function inside the fucntion itself? This just seems wrong but I am looking for a way to search a file structure and can't think of another way off the top of my head. I just see running into issues with variables or getting stuck in an infanate loop or ending loops prematurely ect. Before anyone asks I haven't started yet I am just building a logic tree and I am thinking of using python. Thanks for your help. Link to comment Share on other sites More sharing options...
G+_610GARAGE Posted November 15, 2015 Share Posted November 15, 2015 Don't know anything about python, but I would avoid if at all possible. With C (my favorite language), I believe it is possible, but bad. Since the function is continuously calling itself, it is never removed from the stack, and therefore, eats up all of the stack space and causes weird glitches. I seem to recall having done this before, but can't exactly remember. Could a while loop in one function be used to call the other function? Does python have anything like pointers? When I have to look at a data structure, I'll often use pointers to store the structures locations within the data array. If that makes any sense. Link to comment Share on other sites More sharing options...
G+_Eddie Foy Posted November 15, 2015 Share Posted November 15, 2015 Its not bad. Just need to be certain you won't fall into a inescapable loop. if variables are a possible issue, use objects if you can. If you think about it, all functions are nested in the main loop. Link to comment Share on other sites More sharing options...
G+_Akira Yamanita Posted November 15, 2015 Share Posted November 15, 2015 A function calling itself is called "recursion". It's not bad, per se, but you definitely need to ensure an escape condition otherwise it will loop and run until it crashes. It's also generally considerably slower and more memory intensive than other loops unless the language is optimized for recursion.? Generally speaking, avoid it if you can unless it has significant advantages such as for code readability and the performance hit won't ever be a problem. Link to comment Share on other sites More sharing options...
G+_Ben Reese Posted November 16, 2015 Share Posted November 16, 2015 Looping through subfolders is an excellent use case for recursive functions. Just make sure you have exit points and the fewer variables, the better. Link to comment Share on other sites More sharing options...
G+_Michael Heinz Posted November 16, 2015 Share Posted November 16, 2015 There are definitely times when recursion is a good idea. That said there is almost always a way to write the same code without recursion. Google the phrase tail - end recursion you should find examples. Link to comment Share on other sites More sharing options...
G+_Fr. Robert Ballecer, SJ Posted November 17, 2015 Share Posted November 17, 2015 Don't be afraid of recursion. That being said, avoid this: 10 goto 10 :) Link to comment Share on other sites More sharing options...
Recommended Posts