Lecture2 Part2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Eastern Mediterranean University

School of Computing and Technology


Information Technology
Lecture2 – Functions

Recursive Functions

Recursive

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.

The C programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function,
otherwise it will go into an infinite loop.

Recursive functions are very useful to solve many mathematical problems, such as calculating
the factorial of a number, generating Fibonacci series, etc.

1
Advantages and Disadvantages of Recursion
Recursion makes program elegant and cleaner. All algorithms can be defined
recursively which makes it easier to visualize and prove.
If the speed of the program is vital then, you should avoid using recursion.
Recursions use more memory and are generally slow.

Example1:
The following example calculates the factorial of a given number using a loop:

#include "stdafx.h"
#include "conio.h"
int factorial(int i)
{
int fac = 1;
if (i <= 1) {
return 1;
}
for (int cnt = i; cnt >= 1; cnt--)

fac *= cnt;
return fac;
}

void main() {
int i = 3;
printf("Factorial of %d is %d\n", i, factorial(i));
_getch();
}

The following example calculates the factorial of a given number using a recursive function:
#include "stdafx.h"
#include "conio.h"
int factorial(int);
void main()
{

int fact, n;

printf("Enter any positive integer : ");

scanf_s("%d", &n);

fact = factorial(n);
printf("Factorial of %d is %d", n, fact);
_getch();
}
int factorial(int n)
{

int temp;

if (n == 0)

2
return 1;

else

temp = n * factorial(n - 1); // recursive function call

return temp;
}

3
Example2:
The following example finds Sum of Natural Numbers Using loop

1.way

#include "stdafx.h"
#include "conio.h"

int sum(int n);

void main()
{
int number, result;

printf("Enter a positive integer: ");


scanf_s("%d", &number);

result = sum(number);

printf("sum=%d", result);
_getch();
}

int sum(int n)
{
int x = 0;
for (int i = n; i >=1; i--)
x += i;
return x;
}

2.way

#include "stdafx.h"
#include "conio.h"
void factorial(int n)
{
int result=1;
for (int i = 1; i <= n; i++)
result = result*i;
//return result;
printf("factorial of %d is %d\n", n, result);
}
void main()
{
int n;
printf("enter any positive number:");
scanf_s("%d",&n);
factorial(n); //call by value
_getch();
}

4
The following example finds Sum of Natural Numbers Using a recursive function

#include "stdafx.h"
#include "conio.h"

int sum(int n);

void main()
{
int number, result;

printf("Enter a positive integer: ");


scanf_s("%d", &number);

result = sum(number);

printf("sum=%d", result);
_getch();
}

int sum(int n)
{
if (n != 0)
return n + sum(n - 1); // sum() function calls itself
else
return n;
}

Initially, the sum() is called from the main() function with number passed as an
argument.
Suppose, the value of n is 3 initially. During next function call, 2 is passed to
the sum()function. In next function call, 1 is passed to the function. This process
continues until nis equal to 0.
When n is equal to 0, there is no recursive call and the sum of integers is returned
to the main() function.

5
6
Example3:

Given an integer number and we have to count the digits using recursion using C program.

In this program, we are reading an integer number and counting the total digits,
here countDigits() is a recursion function which is taking number as an argument and
returning the count after recursion process.

Input number: 123


Output:
Total digits are: 3

The following example counts the digits using a loop

#include "stdafx.h"
#include "conio.h"

int countDigits(int num)


{
int count =0;

while (num>0)
{
num = num / 10;
count++;
}
return count;
}
void main()
{
int number;
int count ;

printf("Enter a positive integer number: ");


scanf_s("%d", &number);

count = countDigits(number);

printf("Total digits in number %d is: %d\n", number, count);

_getch();
}

The following example counts the digits using a recursive function

#include "stdafx.h"
#include "conio.h"
/*Static variables are initialized only once.
The compiler persists with the variable till
the end of the program. Static variables can be

7
defined inside or outside the function.
They are local to the block.
The default value of static variables is zero.*/

int countDigits(int num)


{
static int count;

if (num>0)
{
count++;
countDigits(num / 10);
}
else
{
return count;
}
}
void main()
{
int number;
int count = 0;

printf("Enter a positive integer number: ");


scanf_s("%d", &number);

count = countDigits(number);

printf("Total digits in number %d is: %d\n", number, count);

_getch();
}

You might also like