Site icon Codeforcoding

Nesting of function in C programming language

Nesting of function in C programming language

We will discuss about Nesting of function in C programming language

When one or more functions are utilized under a particular function, it is known as nesting function in C Programming Language.

Note – We can not define a function within another function in C language(nested function is not supported by C language). we can only declare a function within another function in C(not define).

Declaration

We can declare of the nesting of function as follows

int main(){//main function in C 
return_type function_name();//Call function under the main function 
return 0; 
}
return_type function_name(){//user defined function //statements of  function 
}
//nesting function in C language

Example

return_type function_one()
{
statements
// statements of function_one
function_two();
//called the function_two inside the function one
}
return_type function_two() //declaration of function two
{
statements
// statements of function_two
 function_three();    //called the function_three
}
return_type function_three()//declaration of function three
{
statements
// statements of function_three
}
int main()
{
 function_one();    //called the function_one
return 0;
}

Example 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("This is main method in c\n");
    function_one();
    getch();
    return 0;
}
void function_one()
{
    printf("This is a user define function\n");
    function_two();
}
void function_two()
{
    printf("This is nested function in c\n");
}

 

 When the above code is executed, it produces the following results:

This is main method in c
This is a user define function
This is nested function in c

 

 

At the above program, we have created two functions with its statements. Then we declare function_two inside the function_one. Afterwards, when we call function_one under the main function,  statements for both function_one and function_two are displayed.

 

Example 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("world countries\n");
    India();
    getch();
    return 0;
}
void India(){
printf("I am india in asia\n");
Srilanka();
}
void Srilanka(){
printf("I am srilanka near the india\n");
printf("I am one of small country in asia\n");
Nepal();
}
void Nepal(){
printf("I am nepal near the india\n");
printf("I am beauty in asia\n");
China();
}
void China(){
printf("I am china in asia\n");
printf("I am powerful in asia\n");
}

 

When the above code is executed, it produces the following results:

world countries
I am in India in Asia
I am in Srilanka near the India
I am in a small country in Asia
I am in Nepal near India
I am beauty in asia
I am china in asia
I am powerful in asia

 

Example 3

Find cube value using the nested function

#include <stdio.h>
#include <stdlib.h>

int cube(int a)//cube function
{
    return a*a*a;
}
void display()//Function to display
{
    int i;
    for(i=1; i<=10; i++){
        printf("cube value of %d is  %d\n",i,cube(i));
    }//cube function called inside the display function
}
int main()
{
    display();
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

cube value of 1 is 1
cube value of 2 is 8
cube value of 3 is 27
cube value of 4 is 64
cube value of 5 is 125
cube value of 6 is 216
cube value of 7 is 343
cube value of 8 is 512
cube value of 9 is 729
cube value of 10 is 1000

 

Example 4

#include <stdio.h>
#include <stdlib.h>

int main()
{
    add();
    getch();
    return 0;
}
int add()  //defined the add function
{
    mul(); //declared of mul function
  int a,b,c;
printf("Enter two numbers to addition :\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("Addition is :%d\n",c);
return 0;

}

int mul() //defined the mul function
{
  int x,y,z;
printf("Enter two numbers to multiplication :\n");
scanf("%d %d",&x,&y);
z=x*y;
printf("multiply is :%d\n\n",z);
return 0;

}

When the above code is executed, it produces the following results:

Enter two numbers to multiplication :
6
5
multiply is :30

Enter two numbers to addition :
20
15

Addition is :35

 

The nested function is not supported in C  language because we cannot define a function inside another function in C language

 

Suggested for you

Function in C language

Function in Python

User define the function in C language

Arithmetic function in C language

If statement in Java language

If statement in C language

If statement in C++ language

If statement in Python language

 

Nested-If  in Java language

Nested-If in C++ language

Nested-If  in Python language

 

Electricity bill calculation program Java language

Electricity bill calculation program  Python language

Electricity bill calculation program C language

Electricity bill calculation program C++ language

Electricity bill calculation program using Java method

Electricity bill calculation program using C function

 

 

User defined function in C programming language
Program to calculate factorial of a number using recursion in Java
Exit mobile version