Search This Blog

Thursday, January 4, 2024

Function in C Part 4: Recursion Function, Nested Functions

 

Recursion Function

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


  • 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.










Nested Functions

GNU C provides several language features not found in ISO standard C.

nested function is a function defined inside another function. Nested functions are supported as an extension in GNU C

The nested function’s name is local to the block where it is defined. For example, here we define a nested function named square, and call it twice:

#include <stdio.h>


int foo (int a, int b);


int main() {

     

    int r;

    r =foo(2,3);

    printf("result :: %d", r);

    

    return 0;

}

int foo (int a, int b)

{

  int square (int z) { return z * z; }


  return square (a) + square(b);

}

Output 

result :: 13


Function in C Part 3:Scope of Variables:

 

Scope of Variables:

  • A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration.

  • The scope of the variable is simply lifetime of a variable. It is block of code under which a variable is applicable or alive. 

  • There are three places where variables you can declare variable programming language:

    1. Inside a function or a block: Local variables

    2. Outside of all functions: Global variables

    3. In the definition of function parameters: Formal parameters

Local Variables:

  • Variables that are declared inside a function or block are called local variables. 

  • The local variables exist only inside the block in which it is declared.

  • The local variable exists until the block of the function is under execution. After that, it will be destroyed automatically.

  • Example of  Local Variable 

public int add(){ 

int a =4; 

int b=5; 

return a+b; 

Here, 'a' and 'b' are local variables

#include<stdio.h>

int main()

{

    int a = 100;

    {

        int a = 10;  

        printf("Inner a = %d\n", a);

    }

    printf("Outer a = %d\n", a);

    return 0;

}


Output:

Inner a = 10

Outer a = 100

Global Variables :

  • Global variables are defined outside a function, usually on top of the program (Global declaration section).

  • It has a global scope means it holds its value throughout the lifetime of the program.

  • Any function can access and modify global variables. That is, a global variable is available for use throughout your entire program after its declaration. 

  • Global variables are automatically initialized to 0 at the time of declaration. 

  • Example:

int a =4; int b=5; 

public int add()

return a+b;

 }

 Here, 'a' and 'b' are global variables.

#include<stdio.h>


void func_1();

void func_2();


int a, b = 10;  // declaring and initializing global variables


int main()

{

    printf("Global a = %d\n", a);

    printf("Global b = %d\n\n", b);


    func_1();

    func_2();


    return 0;

}


void func_1()

{

    printf("From func_1() Global a = %d\n", a);

    printf("From func_1() Global b = %d\n\n", b);

}

void func_2()

{

    int a = 5;

    printf("Inside func_2() a = %d\n", a);

}


Output:


Global a = 0

Global b = 10


From func_1() Global a = 0

From func_1() Global b = 10


Inside func_2() a = 5


Functions in ‘C’ Part 2:Categories of user-defined functions

 

Categories of user-defined functions

All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values.

A function depending on whether arguments are present or not and whether value is returned or not, may belong to one of the following categories:

  1. Function with no arguments and no return value

  2. Function with no arguments and a return value

  3. Function with arguments and no return value

  4. Function with arguments and a return value



  1. Function with no arguments and no return value

When a function has no arguments, it does not receive any data from the calling function. Similarly when it does not return a value, the calling function does not receive any data from the called function.

Syntax:

Function declaration : void function();

Function call : function();

Function definition :

void function()

{

     //statements;

}


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);

    }

}


Output:


Enter 2 numbers that you want to compare...

10 20

The greater number is: 20



  1. Function with no arguments and a return value

There could be occasions where we may need to design functions that may not take any arguments but return a value to the calling function.

An example for this is getchar() function it has no parameters but it returns an integer type data that represents a character.

Syntax:

Function declaration: return_type function ();

Function call: function();

Function definition:

return_type function( ) {

//statements;

return x;

}


Below is an example of a function with no arguments but have return value


// C code for function with no arguments

// but have return value

#include<stdio.h>

int greatNum();       // function declaration

int main()

{

    int r = greatNum();        // function call

    printf("The greater number is: %d", r);

    return 0;

}

int greatNum()        // function definition

{

    int i, j;

    printf("Enter 2 numbers that you want to compare...");

    scanf("%d%d", &i, &j);

    if(i > j) 

        return  i;

    else 

        return  j;

}

Output:

Enter 2 numbers that you want to compare...

10

20

The greater number is: 20

  1. Function with arguments and no return value

When a function has arguments, it receives data from the calling function but it returns no values.


Syntax:

Function declaration :

         void function (data_type arg1,data_type arg2,.. );

Function call :

         function(arg1,arg2,..  );

Function definition:

         void function(data_type arg1,data_type arg2,..  )

         {

               statements;

         }


Below is an example of a function with arguments but no return value


// C code for function with arguments

// and no return value

#include<stdio.h>

void greatNum(int,int);       // 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 n1,int n2)        // function definition

{

    if(n1 > n2) 

       printf("The greater number is: %d", n1);

    else 

        printf("The greater number is: %d", n2);

}


Output:

Enter 2 numbers that you want to compare...

10 20

The greater number is: 20


  1. Function with arguments and a return value

Syntax:

Function declaration :

int function (data_type arg1,data_type arg2,..);

Function call : 

function( arg1, arg2,..  );

Function definition:

return_type function(data_type arg1,data_type arg2,..  )

{

statements;

return x;

}

Below is an example of a function with arguments and return value


// C code for function with arguments

// and return value

#include<stdio.h>

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

int main()

{

    int i, j, r;

    printf("Enter 2 numbers that you want to compare...");

    scanf("%d%d", &i, &j);

    r = greatNum(i,j);        // function call

    printf("The greater number is: %d", r);

    return 0;

}

int greatNum(int n1,int n2)        // function definition

{

    if(n1 > n2) 

       return n1;

    else 

  return n2;

}


Output:

Enter 2 numbers that you want to compare...

10

20

The greater number is: 20


Function in C Part 4: Recursion Function, Nested Functions

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