The concept of Recursion

The concept of Recursion

Recursion, what is it and why use it?

? Recursion is a programming technique using a function that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first.

All of this happens in the stack.

Stack example:

Aucun texte alternatif pour cette image


Why use it?

? Recursion makes many tasks easy especially?for solving problems that can be broken down into smaller, repetitive problems.

Here are some examples for Recursion use cases

? Quicksort Algorithm

? Calculating Fibonacci Sequence

? Calculating Power

? Calculating Factorial

Speaking of Calculating Power, what would it look like to calculate (x) raised to the power of (y) in recursion?

Take this for an example

float _pow_recursion(float x, float y)
{
    if (y == 0)
        return (1);
    if (y < 0)
        return (_pow_recursion(x, y + 1) / x);

    return (_pow_recursion(x, y - 1) * x);
}        

To explain this, it is easiest to show it in a little drawn diagram

Let (x) = 2 and (y) = 13

This is how it would look like recursively

Aucun texte alternatif pour cette image


As we build the stack with the function calls, Upon reaching the condition y == 0

All of the returns of previous functions ripple and get multiplied, then we get our result, in this case: 8192

Hence?_pow_recursion(2, 13) = 8192

要查看或添加评论,请登录

ahmed zghal的更多文章

  • Welcome To Staffy Code Base

    Welcome To Staffy Code Base

    Empowering Your Workforce "Welcome to the future of workplace management with Staffy, the cutting-edge mobile…

    1 条评论
  • What is like being a Designer (UI/UX)

    What is like being a Designer (UI/UX)

    Overview Spotify is a digital music service that gives you access to millions of songs, podcasts, and videos from…

  • Portfolio Project: TunisianDoom

    Portfolio Project: TunisianDoom

    Introduction: this is a simple game made by me Mohamed Ahmed Zghal, it is a game meant to make you have fun and explore…

  • What happens when you type google.com in your browser and press Enter

    What happens when you type google.com in your browser and press Enter

    Nowadays in the digital era, we use the internet for just about everything. We can study, connect with our friends and…

  • Internet of Things (IoT)

    Internet of Things (IoT)

    What is IOT ? The internet of things, or IoT, is a system of interrelated computing devices, mechanical and digital…

  • Python3: Mutable, Immutable... everything is object!

    Python3: Mutable, Immutable... everything is object!

    1 >>> Introduction In object-oriented programming languages like Python, an object is an entity that contains data…

  • The differences between static and dynamic libraries

    The differences between static and dynamic libraries

    Why use libraries? As mentioned before, libraries allow us to store function definitions, aka pieces of code that will…

  • What happens when you type `ls -l *.c` in the shell

    What happens when you type `ls -l *.c` in the shell

    Let’s take a look at what happens when you type ls *.c in the shell.

  • Hard and Symbolic links on Linux

    Hard and Symbolic links on Linux

    What is Linux? Just like Windows, iOS, and Mac OS, Linux is an operating system. In fact, one of the most popular…

  • All the steps of compilation

    All the steps of compilation

    C is a compiled language. Its source code is written using any editor of a programmer’s choice in the form of a text…

社区洞察

其他会员也浏览了