What is recursion?

What is recursion?

The C programming function above calculates the result of exponentiation when given a base and exponent. In the function, x is the base and y is the exponent. The function cleverly calls itself repetitively. In computer programming, this type of function is called recursion. One of the most important components of a recursive function is the base case. Without it, the function will never end because it wouldn't know when to stop. Another important component is the stack in which the function operates on. The stack is a data structure that pushes every call of the function onto it and then, pops them out to return the final value. It follows a LIFO (Last In First Out) order.

In the chart below, x is assigned 3 and y is assigned 3. Since the exponent is a positive number, the function will multiply the base three times. Once the last call is the base case, the stack will pop the return values off until the result of 27 is returned.

No alt text provided for this image

In the chart below, x is assigned 3 and y is assigned -3. Since the exponent is a negative number, the function will divide the base three times. Once the last call is the base case, the stack will pop the return values off until the result of 0.037037 is returned.

No alt text provided for this image

Resources

0. https://www.mathsisfun.com/exponent.html

1. https://www.geeksforgeeks.org/recursion

2. https://www.thecodingdelight.com/understanding-recursion-javascript


To view or add a comment, sign in

More articles by Jennifer Tang

  • Developing My First App: Pollen

    In my professional career, I could count on one hand the few times I jointly demonstrated leadership and technical…

    1 Comment
  • Postmortem: Wordpress 500 Incident

    Issue Summary: The Spanish comment webpage of the company’s website (hosted by Wordpress) threw a 500 Internal Sever…

  • What Happens When You Type an URL on the Browser and Press Enter

    In understanding how the web stack works and its flow, we must first understand the most basic and common term in this…

  • Internet of Things (IoT) 101

    Imagine a world controlled by devices that does not need to be operated by a human and any data collected from its…

  • Machine Learning 101

    Personally, I think everything in the world has a beginning, middle and end. Whether it is a person, a pencil or…

  • Mutable, Immutable... everything is object!

    Everything in Python is an object! It's the first thing I learned about Python and it's the most important takeaway. In…

  • Static vs Shared Library

    Why use libraries? In programming, a library holds functions that are compiled and ready for use. They are stored in…

  • What happens when you type ls -l in the shell?

    One of the most commonly used shell command is ls that displays files and directories in the current working directory.…

  • Build Your Own Static Library

    Why use them? Imagine you move into a new house with a storage closet. Your intention is to store seasonal items like…

  • Hello, World!

    The main goal of compilation is to convert high-level language to low-level language which is synonymous with C…

Explore content categories