Header Ads

3.2.1 - Recursive Function

Function - Recursive 
  • Recursion is the process of repeating items in a self-similar way
  • In programming a function calling itself is called as recursive function
  • Two steps
Step 1: Identification of base case
Step 2: Writing a Recursive case
Example: Recursive Function
Factorial of 3 numbers using Recursive Function..:
#include 
int factorial(int number)
{
    if (number <= 1)
    {
        return 1;
    }
    else
    {
        return number * factorial(number – 1);
    }
}

int main()
{
    int ret;
    ret = factorial(3);
    printf(“Factorial of 3 is %d\n”, ret);
    return 0;
}

Function Pointers
  • A variable that stores the pointer to a function can be referred as Function pointer.
Syntax:            
            datatype (*foo)(datatype, ...);

Variadic Function:
  • Variadic functions can be called with any number of trailing arguments
  • For example: printf(), scanf() are common variadic functions
  • Variadic functions can be called in the usual way with individual arguments
Syntax:   
            return data_type function_name(parameter list, ...);

Variadic - Definition & Usage:
  • Defining and using a variadic function involves three steps:
Step 1: Variadic functions are defined using an ellipsis (‘...’) in
the argument list, and using special macros to access the variable arguments.
Example:               
int foo(int a, ...)
              {
                  /* Function Body */
              }

Step 2: Declare the function as variadic, using a prototype with an ellipsis (‘...’), in all the files which call it.
Step 3: Call the function by writing the fixed arguments followed by the additional variable arguments.
Functions – Variadic – Argument access macros
  • Descriptions of the macros used to retrieve variable arguments
  • These macros are defined in the header file stdarg.h
Type/Macros            Description
  • va_list                  The type va_list is used for argument pointer variables
  • va_start               This macro initializes the argument pointer variable to  point to the first of the optional arguments of the current function; last-required must be the last required argument to the function
  • va_arg                  The va_arg macro returns the value of the next optional argument, and modifies the value of ap to point to the subsequent argument. Thus, successive uses of va_arg return successive optional arguments
  • va_end                  This ends the use of ap

Variadic - Example
#include 
int main()
{
    int ret;
    ret = add(3, 2, 4, 4);
    printf(“Sum is %d\n”, ret);
    ret = add(5, 3, 3, 4, 5, 10);
    printf(“Sum is %d\n”, ret);
    return 0;
}

int add(int count, ...)
{
    va_list ap;
    int iter, sum;
    /* Initilize the arg list */
    va_start(ap, count);
    sum = 0;
    for (iter = 0 ; iter < count; iter++)
    {
        /* Extract args */
        sum += va_arg(ap, int);
    }
    /* Cleanup */
    va_end(ap);
    return sum;
}

...

No comments