Sunday, April 5, 2015

Recursion in Google Apps Script

The recursion is very usefull and probably first hard-to-understand-knowledge for new programmers. It is usefull when you want to to call the same function again and again, but with different values as a parameter.

Well know example is calculating factorial. Factorial of 5, contains factorial  4 and its contains factorial of 3 etc.

5! = 5 * 4! = 5 * 4 * 3! = 5 * 4 * 3 * 1 = 120
4! = 4 * 3! = 4 * 3 * 2! = 4 * 3 * 2 * 1 = 24

So you can write function like

function factorial(n) {
  if (n == 0 || n == 1) return 1;
  if (f[n] > 0)  return f[n];
  return f[n] = n * factorial(n-1);  
} ​
If you run factorial(5), it will call function 5 time. Every new calling will inserted into another.

There is small limitation in Google Apps Script during recursion. You can call only 1000 inherited functions.
If your script call multiple times, you will get error:
Exceeded maximum stack depth 

Are you interested in this topic? Follow me on Twitter or subscribe RSS


  1. This post is very useful for us. Because we have a lot of
    tips and tricks from this post. Thank you for this amazing post share. I many
    tips about < bd jobs as well. If you want to know more about a career sites, please visit our website.

    1. Hello It’s really a nice and helpful piece of info. I am happy that you shared this helpful info with us. Please stay us informed like this. Thank you for sharing. bye
      Tangki Panel
      Tangki Fiberglass
      Jual Septic Tank

  2. You can be sure that you will only get the best online copy watch. Remember that buying fake watches is very similar to buying any other typeReplica watchesReplica Watches Uk

  3. Great work man i would like to congratulate you on this effort

  4. This comment has been removed by the author.

  5. Essentially, it takes your telephone to completely new level and messaging measurement. Trust me, when you've utilized this application for some time, you'll wonder how you lived without it for such a long time!

  6. this is Best free dating apps to meet with other people

  7. replica Alexander McQueen shoes, combining elegant style and cutting-edge technology, a variety of styles of Cheap Alexander McQueen womens shoes, the pointer walks between your exclusive taste style.

  8. A few refreshed telephone accompanying Android stage. More than 500K android apps for more than 500 android gadgets have overflowed by android advertise. Bulk SMS Service Provider in Sharjah UAE

  9. Uploading a mobile application at the store does not guarantee success. Its marketing is equally important, as the audience should come to know about its launch.Mobile App development Companies in UK