Notes on Recursive Function | Grade 12 > Computer Science > Programming in C | KULLABS.COM

Recursive Function

  • Note
  • Things to remember

Introduction

Recurtion can be regarded as the ability of function defining an object in terms of a simpler case of its self. In another words, a function is called recursive if a statement in the body of the function calls itself until some conditions are satisfied. Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied.

The process is used for repetitive computations in which each action is started in terms of the previous result. Many iterative or repetitive problems can be solved using this feature of function.

Design a function that called main() function three times to give same outptut.

#include <stdio.h>

#include <math.h>

#include <stdio.h>

main ()

{

static int z=1;

printf(“\n Example of recursive function “);

z++;

if (z<=3)

main();

else

exit

exit;

}

# Write a program to generate a series of Fibonacci numbers using recursive features of function.

#include <stdio.h>

#include <math.h>

/* compute Fibonacci sequence using recursion */

unsigned uFibr (usigned uNum)

{

if ( uNum == 0)

return 0;

else if (uNum == 1)

return 1;

else return uFibr (uNum -1) + uFibr (uNum-2);

}

int main (void)

unsigned uTnum, uIndex;

printf(“\n enter how many numbers are to be generate:”);

scanf(“%u”, &uTnum);

printf(“n The Fibonacci sequences are:\n”);

for (uIndex =0; uIndex <uTnum; uIndex++)

printf(“\n %u”, uFibr (uIndex));

}

# Write a program to find the greatest common factor or highest common factor of given two positive integers.

/* Greatest common factor (gcd) of given two numbers*/

#include <stdio.h>

unsigned gcd (unsigned, unsigned);

int main (void)

{

int a, b, iGcd;

unsigned gcd (unsigned, unsigned); /* function call */

printf(“/n Type in any 2 numbers whose GCd is to be found :”);

scanf(“%d %d”, &a, &b);

iGcd = gcd (a,b);

printf(“\n Greatest Common Factor GCD of %d and %d is %d \n”, a, b, iGcd);

return 0;

}

unsigned gcd (unsigned p, unsigned q)

{

unsigned remainder;

remainder = p – (p/q*q);

if (remainder ==0)

return q;

else

gcd (q, remainder);

}

# Write a program to find the sum of previous digits using recursive function.

/* function called by reference */

#include <stdio.h>

#include <conio.h>

#include <math.h>

sum (int);

void main () /* function declaration */

{

int z, f;

printf(“ Enter any number “);

scanf(“%d”,&z);

f=sum (z); /* function call and assignment */

printf(“\n Sum = %d, f);

}

sum (int y)

{

int tot;

if (y==1)

return 1;

else;

tot = y+sum (y-1);

return (tot);

}

Note : If the return type is not mentioned, it returns integer type of data. If it is necessary to return value other than int type, then it is compulsory to explicitly mention data type in calling function and call the function. In some programs, an ellipsis (three dots (…)) are used as parameters known as ellipsis operator. This indicates the function may accept any parameters.

Passing Arguments

The value of corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of the argument to a function is known as passing by value.

# Write a program to find the sum of all the digits of the number using function in different forms

  1. Function with no argument and no return;
  2. Function with argument and no return;
  3. Function with no argument but with return;
  4. Function with argument and return;

1. Function with no argument and no return

#include <stdio.h>

#include <conio.h>

sumofdigit ();

main ()

{

sumofdigit ();

}

sumofdigit ()

{

int n, i, r;

float s=0;

printf(“\n Enter a number :”);

scanf(“%d”, &n);

i =n;

while (!i=0)

{

r = i;

s = s+r;

i = i/10;

}

printf(“\n Sum of the digits %5.2f”,s);

/* return 0; */

}

In the above program, sumofdigit() is a function without any parameters in the parenthesis. Hence, it has no arguments. The value or variables included within the parenthesis are called parameters.

The function sumofdigit() contains printf() function which displays the output on the screen. Hence, the main program has to print nothing. The main() calls sumofdigit() function.

2. Function with argument and no return

#include <stdio.h>

#include <conio.h>

sumofdigit (int);

main (0

{

int n;

printf(“\n Enter a number:”);

scanf(“%d”,&n);

sumofdigit (n);

}

sumofdigit (int n)

{

int i,r;

float s=0;

i =n;

while (i!=0)

{

r =i;

s = s+r;

i = i/10;

}

printf(“\n Sum of the digits %5.2f”,s);

/* return 0; */

}

In the above program, sumofdigit(int) is a function with the argument. The argument is int type of data. It accepts integer type of data for the processing. Since, the printf() function is included within the sumofdigit (int n) function, it does not return any values.

3. Function with no argument but with return.

#include <stdio.h>

#include<conio.h>

sumofdigit ();

main ()

{

float s=0;

s = sumofdigit ();

printf(“\n Sum of the digits %5.2f”,s);

}

sumofdigit ()

{

int i, r, n;

float j=0;

printf(“\n Enter a number:”);

scanf(“%d”, &n);

i =n;

while (i!=0)

{

r = i;

j = j+r;

i = i/10;

}

return j;

}

In the above program, sum of digit() is a function without any arguments which is declared above the main () function. The variables holds the values passed by the j from the function. Below is an example of the result of executing the program codes.

4.Function with argument and return

#include <stdio.h>

#include <conio.h>

sumofdigit (int);

main ()

{

int n;

float s=0;

printf(“/n Enter a number”);

scanf(“%d”, &n);

s= sumofdigit (n);

printf(“/n Sum of the digits %5.2f “,s);

}

sumofdigit (int n)

{

int I, r;

float jsum=0;

I =n;

while (i!=0)

{

r = i;

sum = sum+r;

i= i/10;

}

return sum;

}

The above program is similar to the previous programs. In this program, slight modification can be observed. The function sumofdigit (int) definition is parameter or argument within the parenthesis.

The function computes the sum of the individual digits of given number less than 32767 (largest integer value ) and stores in the variable sum. The sum is obtained then passed into the function and then printed with the help of s variable.

Function and Array

Arrays and functions can be used in the program. A program with an array can be passed to the function and processed to obtain the desired result.

# Write a function to print the Fibonacci series up to given number.

#include <stdio.h>

#include <conio.h>

int main ()

{

int n;

void fib (unsigned int);

printf(“\n Number of items to be generated:”);

scanf(“%d”, &n);

printf(“\n Fibonacci sequence up to %d item are:\n”);

fib (n);

return 0;

}

void fib (unsigned int n)

{

static int f1=0, f2=1;

int temp;

if (n<2)

{

f1=0;

f2=1;

}

else

{

fib (n-1);

temp=f2;

f2=f1+f2;

f1=temp;

}

printf(“%d\t”,f1);

return;

}

# Write a function to find the largest number among three input values.

/* largest of three numbers */

#include <stdio.h>

int maximum (int x, int y)

{

int z;

z = (x>=y)? x:y;

return (z);

}

main ()

{

int a, b, c, d;

/* integer values */

printf(“\n First value:”);

scanf(“%d”,&a);

printf(“\n Second value:”);

scanf(“%d”,&b);

printf(“\n Third value:”);

scanf(“%d”,&c);

d=maximum (a,b);

printf(“\n Maximum =%d, maximum(c,d));

}

# Write a program to find the maximum number using a function.

#include <stdio.h>

#include <conio.h>

int maxfunc (int I, int j, int k)

{

int max;

if (i>=j && i>=k)

max =i;

else if (j>=k)

max =j;

else

max =k;

return max;

}

void main ()

{

int max, a, b, c;

printf(“\n Input three nos in single line separated by space:”);

scanf(“%d %d %d”, &a, &b, &c);

max=(maxfunc(a, b, c);

printf(“\n The maximum is %d\n”, max);

}

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 205-213.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd



  1. A function is called recursive if a statement in the body of the function calls itself until some conditions are satisfied.
  2. Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied.
  3. The value of corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change.
  4.  This procedure for passing the value of argument to a function is known as passing by value.
  5. Arrays and functions can be used in the program. A program with an array can be passed to the function and processed to obtain the desired result. 
.

Very Short Questions

0%

ASK ANY QUESTION ON Recursive Function

No discussion on this note yet. Be first to comment on this note