Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6559213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:17:45+00:00 2026-05-25T13:17:45+00:00

I’m trying to learn how to use recursive functions, but am not understanding what

  • 0

I’m trying to learn how to use recursive functions, but am not understanding what is happening at all.

function power(base, exponent) {
    return base * power(base, exponent - 1);
};

alert(power(4,4));

I’m getting:

RangeError: Maximum call stack size exceeded.

From the example that I’m going off of, it has:

function power(base, exponent) {
  if (exponent == 0)
    return 1;
  else
    return base * power(base, exponent - 1);
}

alert(power(4,4));

Could someone explain to me why the if statement is needed? I suspect that I’m missing something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:17:45+00:00Added an answer on May 25, 2026 at 1:17 pm

    A recursive function calls itself. That’s the definition of it.

    This brings a problem: it will indefinitely call itself. So it will go forever which is why you’re getting stack overflows.

    Instead, you should stop at a certain point. This is where the if clause comes in. When exponent == 0, you don’t call the function, but stop the process.

    So when executing power(3, 3) it will go like:

       power(3, 3) is equal to:
    
       3 * power(3, 2)
    or 3 * 3 * power(3, 1)
    or 3 * 3 * 3 * power(3, 0)
    or 3 * 3 * 3 * 1            // no additional calls anymore; can be calculated now
    

    Seen from a little different angle:

    • power(4, 4) is defined as 4 * power(4, 3) by the function.
    • power(4, 3) is defined as 4 * power(4, 2) by the function.
    • power(4, 2) is defined as 4 * power(4, 1) by the function.
    • power(4, 1) is defined as 4 * power(4, 0) by the function.
    • power(4, 0) is defined as 1 by the function.

    If you substitute everything into each previous one, you’ll obtain:

                             power(4, 4)
    equals               4 * power(4, 3)
    equals           4 * 4 * power(4, 2)
    equals       4 * 4 * 4 * power(4, 1)
    equals   4 * 4 * 4 * 4 * power(4, 0)
    equals   4 * 4 * 4 * 4 *      1   
    equals   256
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.