UrbanPro

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How to use recursive function in C?

Asked by Last Modified  

51 Answers

Learn BTech Tuition +1 Pattern Recognition

Follow 0
Answer

Please enter your answer

Tutor

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. But one condition is there, that program must have ending condition to call, other wise...
read more
Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. But one condition is there, that program must have ending condition to call, other wise system will crash. quick example of factorial: int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() { int i = 4; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; } It will produce result 24. read less
Comments

Passionate And Enthusiastic

Recursive functions are those functions who call itself. For example: #include "stdio.h" int main(void) { printf("%d",factorial(5)); return 0; } int factorial(int number){ if(number == 1 || number == 0) return 1; else return number * factorial(number-1); } now in above program can also be written...
read more
Recursive functions are those functions who call itself. For example: #include "stdio.h" int main(void) { printf("%d",factorial(5)); return 0; } int factorial(int number){ if(number == 1 || number == 0) return 1; else return number * factorial(number-1); } now in above program can also be written using while or for loop but WHILE = IF-ELSE in recursion. There must be compulsory at least one terminating condition or base condition. Tracing of above program: F(5) 1) checks if the condition is that passed number is 1 or 0. 2) if condition fails it goes to else block and return number * fact(number-1) in this case return 5 * fact(4); return 4 * fact(3); return 3 * fact(2); return 2 * fact(1); now the number = 1 and as soon as if condition get true it will return value 1; return 2 * 1; return 3 * (return 2 * 1 - returned value); and like wise. read less
Comments

Home Tutor

Function which calls itself like factorial of a number.
Comments

Gateway2IT driven trainings by Industry expert working professionals

Recursive function means a function which call itself and should have at least one exist condition. Without exit condition this type of function call goes infinitely which will leads to application hang-up OR in deadlock state. Recursive function uses STACK data structure i.e. LIFO Concept (Last-in First...
read more
Recursive function means a function which call itself and should have at least one exist condition. Without exit condition this type of function call goes infinitely which will leads to application hang-up OR in deadlock state. Recursive function uses STACK data structure i.e. LIFO Concept (Last-in First out). For Ex - To evaluate factorial value using Recursion main() { int num; printf("Enter a number to evaluate its factorial : "); scanf("%d", &num); printf("Factorial is : %d", fact(num)); // Calling fact function to calculate factorial of a number } int fact(int num) { if(num >) { return (num * fact(num - 1)); // Calling itself } else { //Exit Condition from Recursive function return 1; } } Let's Suppose User enter 5 and it is stored in num. DRY RUN for above Recusive fucntion calls is: num return (num * fact(num - 1)) Stored in STACK 5 5 * fact(4) 4 4 * fact(3) 3 3 * fact(2) 2 2 * fact(1) So following return statement stored into STACK is: 5 * fact(4) 4 * fact(3) 3 * fact(2) 2 * fact(1) and finally evaluated in reverse direction as per STACK data structure: return 2 * 1 = 2 return 3 * 2 = 6 return 4 * 6 = 24 return 5 * 24 = 120 So final result of this program is- Factorial is : 120 read less
Comments

Tutor

When a function calls itself, then that function is referred as recursive function. Example: void rec( ) { // Recursive function named rec( ) rec( ); // Function named rec( ) is calling itself } int main( ) // main( ) function { rec( ); // function named rec( ) is called for the first...
read more
When a function calls itself, then that function is referred as recursive function. Example: void rec( ) { // Recursive function named rec( ) rec( ); // Function named rec( ) is calling itself } int main( ) // main( ) function { rec( ); // function named rec( ) is called for the first time from main( ) function } read less
Comments

Be Good Do Good

When ever there is a requirement of performing same task number of times we will go for recursion. Calling function with in it is called recursive function we use this recursion in divide and conquer, tree traverse these are the few examples.
Comments

Tutor

Recursion means the function repeats until the professor completes.
Comments

Tutor

Recursion means the function repeats until the process completes.
Comments

Tutor

int main() { static int i; printf("pragati"); if(++i<10)main(); return 0; } o/p: pragati 10 times. description: i should be static so that each time main will be called, it will not be re initialized. if it will be so, code will be dumped after few iteration.
Comments

I hope you do know what if recursive function. In programming language recursive function is a function which calls itself. Any repetitive task can be accomplished using recursive function. The main concern about writing a recursive function is to define the exit condition from the repetitive task. Let...
read more
I hope you do know what if recursive function. In programming language recursive function is a function which calls itself. Any repetitive task can be accomplished using recursive function. The main concern about writing a recursive function is to define the exit condition from the repetitive task. Let us take an example of factorial problem. Mathematically, factorial(N) = N * factorial(N-1). that is the function calls the same function by reducing the value by 1. Now here the repetitive process ends when the argument becomes 1 or 0 because factorial(1) = 1 and factorial(0) = 1, here it does not require to call the same function. So this the exit condition for that particular problem. we can write the function in C like this int factorial (int arg) { if(arg == 0 || arg == 1) // exit condition return 1; else return arg * factorial(arg - 1); } read less
Comments

View 49 more Answers

Related Questions

Why density of Ice is lesser than water?
The main reason why the density of ice is lesser than that of water is because of its crystal structure. Four hydrogen atoms tetrahedrally surround each oxygen atom in the ice. Two of the hydrogen atoms...
Bitu
What are the future prospects of doing a BTech in marine engineering from Indian Maritime University?
You will be able to apply your knowledge which you have gained at the university to solve real-world problems in the marine industry and also develop new and innovative projects in this field. You may...
Preeti
0 0
6

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Semiconductors in a nutshell
What are semiconductors? Semicondutcors are substances which have conductivity in between conductors and insulators. Example: Germanium and Silicon. What are the types of semiconductors? Intrinsic semiconductors:...

Learning Path For Mobile Application Development
I. Traditional Approach: 1. Native Android Application Development: Android is a mobile operating system that runs on phones, tablets, smartwatches, TVs and more. You can learn Android Application Development...

Protocol: Preparation for Ultra competent cells of E.Coli DH5-alpha
Preparation: Prepare LB media and Transformation Buffer. Transformation Buffer 10 mM Hepes15 mM CaCl2250 mM KCl (pH to 6.7 with KOH)MIX Thoroughly with constant stirring and than add 55 mM MnCl2 and...


Program Implementation
To implement a program, a student must understand the fundamental programming constructs of the language, as well as the design of the program. Object-oriented development involves implementing the individual...

Recommended Articles

According to a recent survey conducted by the NCAER (National Council of Advanced Economic Research), engineering is the most sought after course in India. Some engineering courses are offered as BE or Bachelor of Engineering while some as Bachelor in Technology or B.Tech. Since engineering is a professional course, the...

Read full article >

MBA, Medicine and engineering are the three main professional courses in India. Engineering is still one of the highly sorted after professional courses in the under graduate level, while MBA is favoured as a preferred post graduate course. To shine in these courses, one needs to work and study hard. Engineering as a...

Read full article >

With the mushrooming of international and private schools, it may seem that the education system of India is healthy. In reality, only 29% of children are sent to the private schools, while the remaining head for government or state funded education. So, to check the reality of Indian education system it is better to look...

Read full article >

Learning for every child starts from a very young age. While the formal methods include school curriculums and private lessons, the informal methods include dancing, music, drawing, and various fun-filling activities. Playing games and practising these types of activities helps the children get out of boredom and...

Read full article >

Looking for BTech Tuition ?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for BTech Tuition Classes?

The best tutors for BTech Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BTech Tuition with the Best Tutors

The best Tutors for BTech Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more