P. 1
Recursion in C

Recursion in C

|Views: 14|Likes:
Published by Rakesh Patel

More info:

Published by: Rakesh Patel on Aug 14, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/03/2014

pdf

text

original

Recursion (Recursive Function

) :
When a function of body calls the same function then it is called as 'recursive function.' Recursion is a procedure or subroutine, implemented in a programming language, whose implementation references itself. Most high-level computer programming languages support recursion by allowing a function to call itself within the program text.

Recursion Recursive functions are those functions, which call itself within that function. A recursive function must have the following type of statements. • A statement to test and determine whether the function is calling itself again. • A statement that calls the function itself and must be argument. • A conditional statement (if-else) • A return statement. Example: Factorial of a number This is the most famous program on recursion. Many versions of this program are available. All programs differ only in checking conditions. I prefer to write like the following one.
Example: Recursion() { printf("Recursion !"); Recursion(); }

Program:

// Program to demonstrate function recursion. #include <stdio.h> #include <conio.h> Recursion() { int no; printf("\nRecursion... "); printf("\n Enter Number : "); scanf("%d",&no); if (no==3) exit(0); else Recursion(); } void main() { clrscr(); Recursion(); }
Output:

Recursion... Enter Number : 2 Recursion... Enter Number : 1 Recursion... Enter Number : 3_

Features:
• • • • •

There should be at least one if statement used to terminate recursion. It does not contain any looping statements. It is easy to use. It represents compact programming structures. It is slower than that of looping statements because each time function is called.

Advantages: Disadvantages:

Note:

It can be applied to calculate factorial of a number, fibonancci series.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->