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

what is oops
object oriented programming
Abhishek
Can I manage class 11 with entrance coaching?
yes , of course you can do topic as it is taught in class in parallel way.
Mukund
0 0
6
How should I study by my own in class 11 and 12 without coaching?
1. well planned study, 2. by making time table and strictly follow it, 3. for doubts you can clear it from internet, school teachers, seniors, friends, elders etc.
Bivash
0 0
5
How can I study for IIT preparation from class 11 without coaching?
Follow Arihanth book series. Solve each and every problems. Check the syllabus on net. Follow YouTube. And take best faculties in your home.
Sweetu
How long should a student of class 9 practice math?
For an average and a slow learner 2 hours per day is required whereas for a above average just enough to have at glance. It depends on the strengths of the child on the subject.
Bhushan
0 0
5

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

Ask a Question

Related Lessons

The Best Option JavaScript Language For Developers
We already know about JavaScript. But today I will be discussing JavaScript features and JavaScript development process. I was wonder about JavaScript last 10 years. Because JavaScript can give us frontend...

Learn by Examples
To get any perticular concept of any subject, there are few basic steps. Take the problem based on the concept you are trying to learn. Read the problem and identify the context Check whether you...
P

Capacitor Construction
• A capacitor is constructed using a pair of parallel conducting plates separated by an insulating material (dielectric). • When the two plates of a capacitor are connected to a voltage source...

10 Reasons Why Big Data Analytics Is The Best Career Move?
Why Big Data Analytics is the Best Career move? If you are still not convinced by the fact that Big Data Analytics is one of the hottest skills, here are 10 more reasons for you to see the big picture: 1....

Types of Open Channel Flow
Types of Open Channel Flow 1 Steady Flow Flow is said to be steady when discharge does not change along the course of the channel flow. 2 Unsteady Flow Flow is said to be steady when discharge changes...

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