Copyright

Practical Application for C Programming: Recursive Functions

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this practical application lesson, you will write working C code. You will write and edit functions that take advantage of recursion. You will then be able to develop, run, and test your code.

Lesson Overview & Knowledge Required

In order to successfully complete this lesson, you should be able to describe recursion in the C computer programming language. You should be able to write, compile and test programs in C, as well as declare, create and call functions using C.

Program Code

A recursive function is one that calls itself. In order to do this, however, you always need a way out of the function or you will be trapped and the program will most likely crash.

Fibonacci Series

For this program code, you will calculate a Fibonacci sequence. This sequence is defined as follows:

  • The sum of two numbers preceding the current number equal that number
  • The first numbers are 0, 1, 1, 2
  • In the above sequence: 1 = 0 + 1, 2 = 1+1, and so on

Let's look at the first 20 numbers in the Fibonacci sequence. To do so, we will write a recursive C program. Open your developer tool (e.g. Dev C++) and enter the following code:

#include <stdio.h>
//declare the function
int fibo(int num);
//main function
int main() {
 printf("*** Fibonacci Sequence ***\n");
 int num;
 for(num = 0; num < 10; num++) {
  //call the function
  printf("%d\t", fibo(num));
 return 0;
}
//Recursive function here:
int fibo(int num) {
 if(num == 0) {
  return 0;
 if(num == 1) {
  return 1;
 return fibo(num - 1) + fibo(num - 2);
}

The function fibo calls itself until it has found the proper sequence.

Code Application

Now it is your turn. Instead of finding a Fibonacci sequence, find the sum of natural numbers for a given input. This means if the user enters 10, you would sum 1 + 2 + 3 + 4 + 5 + 6 +7 + 8 + 9 + 10 = 55.

We'll get you started by creating the main function that collects input from the user and calls the function. You will need to complete the function declaration and the function definition.

#include <stdio.h>
int main() {
 int num, result;
 printf("Enter a positive value");
 scanf("%d", &num);
 result = sum(number);
 printf("sum = %d", result);
 return 0;
}

Follow-Up Questions

Now that you have completed the code application, answer the following questions.

Question 1

What happens if you enter a very large number for the input, say 1,000,000? How can you fix this?

Question 2

What would happen if the only code in the sum function was to return the value if i + sum(i-1)?

Question 3

The following recursive function isn't working. The intent is to find the factorial of a number. For example, the factorial of 3 is 3 * 2 * 1 = 6. Correct the code, compile and run.

#include <stdio.h>
long fact(long f_num);
int main(void) {
 long tester = 8;
 printf("Factorial of %d is %d\n", tester, fact(tester));
}
long fact(long f_num) { 
 long number;
 number = fact(f_num-1) * f_num;
}

Answer Key

Following are the answers to the code application and the follow-up questions.

Code Application

Below is the full code for the working C program:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support