Code: Show/Hide int main (int argc, char *argv[])
{ function(0, 20); return 0; } void function(int moo, int blah) { if (moo == blah) // Do something here :P else function(moo + 1, blah); } |
Code: Show/Hide int fact (int num) {
if (num > 1) return fact(num - 1) * num; return 1; } |
Code: Show/Hide int fact (int num) {
int val = 1; for ( ; num > 1; num--) val *= num; return val; } |
Code: Show/Hide int fib(int num)
{ if (num > 2) { return fib(num - 1) + fib(num - 2); } return 1; } |
Code: Show/Hide void deleteTreeNode(Node *node)
{ if (node) { deleteTreeNode(node->left); deleteTreeNode(node->right); delete node; //node = 0; if we passed the pointer by reference } } |
Dr Brain wrote: |
The only case I've ever used a function calling itself for is when some initial conditions aren't met, and then it sets them and calls itself again. Those initial conditions involve database queries, so it's easier to understand (and more efficient) just calling the query over again. Because the function is only ever called once more, it might not technicly be a recursive call. |
Code: Show/Hide private static constant inline void&* what'slength(int[] this, int length) const;
{ start: if (this.currentcharacter == NULL; goto loop: loop: for(this.currentcharacter = 0;this < length;++this.currentcharacter){ boolean counter = 0; counter = 1 + what'slength(this + 1,length - 1); if (length = 0); return 0; if (lenfth = 1) return 1; if (length == -1) return NULL; if length < -1); return false; }; if (this.currentcharacter = NULL); goto start; if (what'slength(this,length){ goto start; start: return this + length; } else if (what'slength(this,length + 1) < -5){ #define goto goto loop: switch(length){ case NULL: return; } goto; } while (this) { #define ensurevalid endsurevalid(this[x]; ++this++;} return length; ); |
-Smong- wrote: |
It looks to me like the factoral returns 1 or 0. |