Categories

## Recursion

A function that calls itself is known as a recursive function. And, this technique is known as recursion.

### How recursion works?

```void recurse()
{
... .. ...
recurse();
... .. ...
}

int main()
{
... .. ...
recurse();
... .. ...
}```

The recursion continues until some condition is met to prevent it.

To prevent infinite recursion, if…else statement (or similar approach) can be used where one branch makes the recursive call, and other doesn’t.

### Example: Sum of Natural Numbers Using Recursion

``````#include <stdio.h>
int sum(int n);

int main() {
int number, result;

printf("Enter a positive integer: ");
scanf("%d", &number);

result = sum(number);

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

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

Output

```Enter a positive integer:3
sum = 6```

Initially, the `sum()` is called from the `main()` function with number passed as an argument.

Suppose, the value of n inside `sum()` is 3 initially. During the next function call, 2 is passed to the `sum()` function. This process continues until n is equal to 0.

When n is equal to 0, the `if` condition fails and the `else` part is executed returning the sum of integers ultimately to the `main()` function.

Recursion makes program elegant. However, if performance is vital, use loops instead as recursion is usually much slower.

That being said, recursion is an important concept. It is frequently used in data structure and algorithms. For example, it is common to use recursion in problems such as tree traversal.

Categories

## Types of User-defined Functions in C Programming

These 4 programs below check whether the integer entered by the user is a prime number or not.

The output of all these programs below is the same, and we have created a user-defined function in each example. However, the approach we have taken in each example is different.

## Example 1: No arguments passed and no return value

``````#include <stdio.h>

int main()
{
checkPrimeNumber();    // argument is not passed
return 0;
}

// return type is void meaning doesn't return any value
{
int n, i, flag = 0;

printf("Enter a positive integer: ");
scanf("%d",&n);

for(i=2; i <= n/2; ++i)
{
if(n%i == 0)
{
flag = 1;
}
}
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
}
``````

The `checkPrimeNumber()` function takes input from the user, checks whether it is a prime number or not and displays it on the screen.

The empty parentheses in `checkPrimeNumber();` statement inside the `main()` function indicates that no argument is passed to the function.

The return type of the function is `void`. Hence, no value is returned from the function.

## Example 2: No arguments passed but a return value

``````#include <stdio.h>
int getInteger();

int main()
{
int n, i, flag = 0;

// no argument is passed
n = getInteger();

for(i=2; i<=n/2; ++i)
{
if(n%i==0){
flag = 1;
break;
}
}

if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);

return 0;
}

// returns integer entered by the user
int getInteger()
{
int n;

printf("Enter a positive integer: ");
scanf("%d",&n);

return n;
}
``````

The empty parentheses in the `n = getInteger();` statement indicates that no argument is passed to the function. And, the value returned from the function is assigned to n.

Here, the `getInteger()` function takes input from the user and returns it. The code to check whether a number is prime or not is inside the `main()` function.

## Example 3: Argument passed but no return value

``````#include <stdio.h>
void checkPrimeAndDisplay(int n);

int main()
{
int n;

printf("Enter a positive integer: ");
scanf("%d",&n);

// n is passed to the function
checkPrimeAndDisplay(n);

return 0;
}

// return type is void meaning doesn't return any value
void checkPrimeAndDisplay(int n)
{
int i, flag = 0;

for(i=2; i <= n/2; ++i)
{
if(n%i == 0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not a prime number.",n);
else
printf("%d is a prime number.", n);
}
``````

The integer value entered by the user is passed to the `checkPrimeAndDisplay()` function.

Here, the `checkPrimeAndDisplay()` function checks whether the argument passed is a prime number or not and displays the appropriate message.

## Example 4: Argument passed and a return value

``````#include <stdio.h>

int main()
{
int n, flag;

printf("Enter a positive integer: ");
scanf("%d",&n);

// n is passed to the checkPrimeNumber() function
// the returned value is assigned to the flag variable

if(flag == 1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);

return 0;
}

// int is returned from the function
{
int i;

for(i=2; i <= n/2; ++i)
{
if(n%i == 0)
return 1;
}

return 0;
}``````

The input from the user is passed to the `checkPrimeNumber()` function.

The `checkPrimeNumber()` function checks whether the passed argument is prime or not.

If the passed argument is a prime number, the function returns 0. If the passed argument is a non-prime number, the function returns 1. The return value is assigned to the flag variable.

Depending on whether flag is 0 or 1, an appropriate message is printed from the `main()` function.

## Which approach is better?

Well, it depends on the problem you are trying to solve. In this case, passing argument and returning a value from the function (example 4) is better.

A function should perform a specific task. The `checkPrimeNumber()` function doesn’t take input from the user nor it displays the appropriate message. It only checks whether a number is prime or not.