Nesting of Function in C

theteche.com
1 min readJun 9, 2021

C permits nesting of function in c freely. main can call function1, which calls function2, which calls function3, ….. and so on. There is principle no limit as to how deeply functions can be nested.

Consider the following program

float ratio (int x, int y, int z);
int difference (int x, int y);
main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%f \n", ratio(a,b,c));
}
float ratio(int x, int y, int z)
{
if(difference(y, z))
return(x/(y-z));
else
return(0.0);
}
int difference(int p, int q)
{
if(p !=q)
return (1);
else
return(0);
}

The above program calculates the ration

and prints the result. we have the following three functions main()

ratio()

difference()

main reads the values of a, b and c and calls the function ratio to calculate the value a/(b-c). The ratio cannot be evaluated if (b-c) = 0. Therefore, ratio calls another function difference to test whether the difference (b-c) is zero or not; difference returns 1, if b is not equal to c; otherwise return zero to the function ratio. In turn, ratio calculates the value a/(b-c) if it receives 1 and returns the result in float. In case, ratio receives zero from difference, it sends back 0.0 to main indicating that (b-c) = 0.

More : Click here

--

--