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 tie the digital electronics soon problem?
Use analog tie set solution.
RAJARAJESWARIRAMARAJ
Why electrical energy playing a major role compare to various other forms of energies that exist in the world?
Because it can also genrates the other forms of energy like heat, light etc.
Saikrishna
0 0
8
Is it good to take 2 maths tuition in class 10?
No. Concentrate and clarify all your doubts with only one tution teacher.
Y
0 0
5
How many laws are defined in thermodynamics?
Basically classical thermodynamics have three laws in which 1st is CONSERVATION OF ENERGY ,2 ENTROPY principal which is also known as qualitative law n 3rd is 0 KELVIN temp CALLED absolute zero can not be achieved
Srinivas

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

Ask a Question

Related Lessons

FUNDAMENTALS OF COMPUTER ALGORITHMS
INTRODUCTION: The word algorithm is named after the ninth century scholar 'Abu Jafar Muhammad Ibn Musa Al-Khwarizmi'. An algorithm is a step-by-step procedure by which a computer can produce the required...
M

Mukesh Tekwani

2 0
0

Pointers and References
Are reference and pointers same? No. I have seen this confusion crumbling up among the student from the first day. So better clear out this confusion at thevery beginning. Pointers and reference...

Engineering Graphics
It's very simple to learn Eng.graphics.But the one and only toughest thing is getting the concept or idea .So start learning by considering the real objects and it's views. All the best.

A Brief Intro into Electromagnetism
The topics covered in this part are given below: • Oersted’ Experiment • Magnetic Field • Moving Charge & Magnetic Field • Lorentz Force •...

Oncolytic virotherapy
Oncolytic Virotherapy: Measles Virus as an agent of tumour destruction Oncolytic Viruses- Viruses that replicate selectively in tumour tissues and lyse the tumour cells Virotherapy Benefits (Attenuated...

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