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

Would it be good for me if i do masters from abroad(maybe canada) after doing btech in cs from CCET chandigarh(comes under PUNJAB UNIVERSITY)?What would be the scope for my career after graduating?
It is always good to do masters in Canada after doing your B.Tech in CSE from CCET Chandigarh.Reason:The cost of education is much cheaper in Canada compared with that of US universities.You will get good...
Nitin
Is there any vacancy in any engineering or polytechnic college in Sangli/Miraj? I have completed M.Tech in ece.
Hi Nupur, it is always best to contact the institutes directly and send them your CV. For getting information you just have to google with the phrase "engineering and polytechnic college in Sangli, Miraj",...
Nupur
3 0
9
How can i get cctv and electronics devices technical notes and text in newmumbai and mumbai area... Please help me to learn ethivally thankyou.
Not only in Mumbai you can get it every where you want if u use internet for learning these subjects
Matrix
0 0
8
I have just entered class 11 and want to prepare for IIT-JEE. How do I manage between school and coaching?
Focus on JEE classes as most of the students do. You can cover your class course easily after these classes before exams.
Sweetu
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

Engineering Safety
For safety, speed and fuel efficiency, when designing recreation and transportation vehicles, engineers take into account all forces acting on the object. Engineers consider drag forces when they design...

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...

Easy way to remember Java keyword.
ACCESS MODIFIER ACCESS SPECIFIER abstract, assert, const, final, native, static, strictfp, super, synchronized, this, transient, void, volatile public, private, protected, default DATA...

Rohit Deshbhratar

1 0
0

Compiling C program on Linux machine GCC ,GDB
Linux have powerful tool called "Terminal". From "terminal" we can command the machine. For any programming language the required tools are editor, compiler and debugger. Compiler will check the syntax...
N

Narasimha Reddy

0 0
0

Big Data for Beginners
Hello Big Data Enthusiast, Many of you would have heard about this term "Big Data" getting buzzed out everywhere and wondering what it could be. Ok, let's sort out things with an example. Imagine you...

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