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

How do I manage school, coaching, home tuitions in class 12?
You have to plan your study and manage time by yourself for this you should understand and analyse what is important and what is not to achieve your goal. Make schedule or time table for utilizing your time in best manner.
Rosary
0 0
6
How to study the diploma engg drawing papers
You should start with lettering & drawing lines and planes with HB or H pencil and neatness of drawing will have special advantage. Then learn different methods of projection and sectioning. Once you are...
Amar
2 0
6
Which language is useful for android app.development?
You must have basic knowledge on Java to work on Android Programming.
Gowthamreddy

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

Ask a Question

Related Lessons

Few Operating System Question - Answers for Fresher Level Interview
What is kernel? How it is different from shell? Kernel is the core portion of the Operating system which gets loaded into the primary memory at the time of bootstrapping. It allocates the memory for the...

Estimation of Water and Wastewater Flow
Before designing the proper waterworks project, it is essential to determine the quantity of water that is required daily. This involves the determination of the population and the rate of demand. The...

Java: A Quick Overview
Not purely Object Oriented: It doesn't support multiple inheritence, it supports primitive data types and static members. Doesn’t support multiple inheritance: Reason is diamond problem i.e.,...

Facebook Analytics
Assume how the Facebook application will store the millions of customer's record in real-time: facebook = { 'jose': { 'name': 'jose', 'age': 33, 'hobby': , # cricket,football 'mobile': 1111111111, 'email':...

Some Interview Questions And Answers For Fresher Level On Pointers
What is a void pointer? Void pointer is a special type of pointer which can reference or point to any data type. This is why it is also called as Generic Pointer. As a void pointer can point to any...

Recommended Articles

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 >

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 >

While schools provide formal education to the children, the home is where they start learning about things informally. Parents think that schools will take the initiative to educate their children. Well, this is partially true, as parents also play an essential role in bringing up their child. For the development of particular...

Read full article >

Quality education does not only help children to get a successful career and life, but it also hugely contributes to society. The formal education of every child starts from school. Although there are numerous schools, parents find it challenging to choose the right one that would fit their child. It is difficult for them...

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