Jump to content

Question: is it okay to nest a call to a function inside the fucntion itself?


G+_Jason Perry
 Share

Recommended Posts

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

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

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

 Share

×
×
  • Create New...