function in C

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

 

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

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago