Feb 5, 2020

Type of User-defined Functions in C, Recursion


                Unit IV (Contd…..)


              Various categories of functions, Nesting of functions and recursion.


            Type of User-defined Functions in C

There can be 4 different types of user-defined functions, they are:

·        Function with no arguments and no return value
·        Function with no arguments and a return value
·        Function with arguments and no return value
·        Function with arguments and a return value

           Below, we will discuss about all these types, along with program examples.

          Function with no arguments and no return value

Such functions can either be used to display information or they are completely dependent on user inputs.
Below is an example of a function, which takes 2 numbers as input from user, and display which is the greater number.
 
#include<stdio.h>
 
void greatNum();       // function declaration
 
int main()
{
    greatNum();        // function call
    return 0;
}
 
void greatNum()        // function definition
{
    int i, j;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    if(i > j) {
        printf("The greater number is: %d", i);
    }
    else {
        printf("The greater number is: %d", j);
    }
}

           Function with no arguments and a return value

We have modified the above example to make the function greatNum() return the number which is greater amongst the 2 input numbers.
#include<stdio.h>
 
int greatNum();       // function declaration
 
int main()
{
    int result;
    result = greatNum();        // function call
    printf("The greater number is: %d", result);
    return 0;
}
 
int greatNum()        // function definition
{
    int i, j, greaterNum;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    if(i > j) {
        greaterNum = i;
    }
    else {
        greaterNum = j;
    }
    // returning the result
    return greaterNum;
}

             Function with arguments and no return value

We are using the same function as example again and again, to demonstrate that to solve a problem there can be many different ways.
This time, we have modified the above example to make the function greatNum() take two int values as arguments, but it will not be returning anything.

#include<stdio.h>

void greatNum(int a, int b);       // function declaration

int main()
{
    int i, j;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    greatNum(i, j);        // function call
    return 0;
}

void greatNum(int x, int y)        // function definition
{
    if(x > y) {
        printf("The greater number is: %d", x);
    }
    else {
        printf("The greater number is: %d", y);
    }
}

           Function with arguments and a return value

This is the best type, as this makes the function completely independent of inputs and outputs, and only the logic is defined inside the function body.

#include<stdio.h>

int greatNum(int a, int b);       // function declaration

int main()
{
    int i, j, result;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    result = greatNum(i, j); // function call
    printf("The greater number is: %d", result);
    return 0;
}

int greatNum(int x, int y)        // function definition
{
    if(x > y) {
        return x;
    }
    else {
        return y;
    }
}

          Nested functions in C

In some applications, we have seen that some functions are declared inside another function. This is sometimes known as nested function, but actually this is not the nested function. This is called the lexical scoping. Lexical scoping is not valid in C because the compiler is unable to reach correct memory location of inner function.
Nested function definitions cannot access local variables of surrounding blocks. They can access only global variables. In C there are two nested scopes the local and the global. So nested function has some limited use. If we want to create nested function like below, it will generate error. 

          What is Recursion?

Recursion is a special way of nesting functions, where a function calls itself inside it. We must have certain conditions in the function to break out of the recursion, otherwise recursion will occur infinite times.

          Example: Factorial of a number using Recursion

#include<stdio.h>

int factorial(int x);       /*declaring the function*/

void main()
{
    int a, b;
   
    printf("Enter a number...");
    scanf("%d", &a);
    b = factorial(a);       /*calling the function named factorial*/
    printf("%d", b);
}

int factorial(int x)          /*defining the function*/
{
    int r = 1;
    if(x == 1)
        return 1;
    else
        r = x*factorial(x-1);       /*recursion, since the function calls itself*/
   
    return r;
}

No comments:

Post a Comment