Recursive Functions In C Programming Language


Lets learn recursive functions in C programming language, with examples and illustrations of memory and function instances in the stack.

Stake is a Data Structure and we’ll be discussing it once we start teaching Data Structures using C topics. For now consider it as a memory stack and some organized structure to hold data, which follows LIFO rule. i.e., Last In, First Out

That is, the last instance which enters the stack is the one which leaves the stack first.

Related Read:
Function / Methods In C Programming Language

Recursive Function: A function calling itself, within it’s own function definition is called Recursive Function.

Types of Recursive Functions

There are 4 types of recursive functions:
1. Direct Recursive function.
2. In-direct Recursive function.
3. Tail Recursive function.
4. Non-tail Recursive function.

We’ll discuss each one in separate video tutorials.

In this video tutorial lets learn the very basic of how to write and use recursive functions and how to keep track of function instances and return data.

Requirements To Learn Recursion

1. You need to know basics of C programming language. If you’re a total beginner, then we do not advice you to start with recursive functions. Just go to this page C Programming: Beginner To Advance To Expert and start watching the C program video tutorials one by one, and in no time you’ll be an advance user and you can learn/understand Recursive functions.

2. If you know the basics, then grab a piece of paper and get a pen. Write down the program on the paper, and also write the function call instances and track the variable values. Write everything we’re explaining in this video tutorial. If you get any doubts, write it down too. Watch the video once again and check if your doubts are cleared. If its still not cleared, follow the next step.

3. Turn on your computer and your favorite C editor, and type the program. Don’t copy paste it. Write the code yourself. Better if you write it without looking at the source code we’ve posted below. Once you execute and get the results, edit/modify the source code and check for different results. Check if you can edit the program and clarify your doubts. If you still don’t understand, then use the comment section below and ask your doubts.

4. Even if you understood the concept well, please visit the comment section below and try to help other students understand it. By teaching, you learn more.

Video Tutorial: Recursive Functions In C Programming Language


[youtube https://www.youtube.com/watch?v=tUQw_ty97yQ]

YouTube Link: https://www.youtube.com/watch?v=tUQw_ty97yQ [Watch the Video In Full Screen.]


Source Code: Recursive Functions In C Programming Language: Example

#include<stdio.h>

void natural(int);

int main()
{
    int num = 1;

    natural(num);

    return 0;
}

void natural(int num)
{
    if(num <= 3)
    {
        natural(num+1);
        printf("%d  ", num);
    }

    return;
}

Output:
3 2 1

Logic To Print natural numbers from 1 to 3 using Recursion

We call natural() function and pass num to it. Initial value of num is set to 1. Inside natural() function/method we check if the value of num is less than or equal to 3. If true, we call the method natural() and pass num+1 to it. Once num is greater than 3, return statement executes and the control transfers to the calling function and the remaining code in that function gets executed. In our program we have printf statement after the recursive function call. So our program prints the value of num and then transfers the control to the calling function.

SlnonumFunction Call
11main()
21nature(1+1)
32nature(2+1)
43nature(3+1)
54nature(4+1)

Each time the recursive call is executed, an instance of the function and memory associated with it is pushed or added to the stack. And for each time the return statement is executed, the function and the memory associated with it is popped or deleted from the stack.

Stack is a Data Structure used to store data in some organized way. For now just know that its a memory stack in your RAM. It’s like a basket which holds the data in particular order. The data is stored one upon another. So the one which gets inserted or pushed at the last is the one which gets popped out first. This concept is called LIFO. Which means, Last In, First Out.

5
4
3
2
1

So we have 5 numbers inside the stack. If we want to add 6 to it, it’ll sit at the top of 5. In above stack, if you want to pop out, the first number you can pop out is the last number present in the stack, which is 5. You can only pop out the items one by one in this order, from above stack: 5, 4, 3, 2, 1.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *