How does the function call within function get evaluated?

Whenever we have more than one function which is called for a finite number of times then such a function gets evaluated from inside out.

Let us understand this concept with an example.

For instance consider a function sample called within it 4 times as given in program below:

main()
{
int a=50;
a=sample(a=sample(a=sample(a=sample(a))));
printf(“%d”,a);
}

sample(a1)
int a1;
{
a1= a1+10;
return(a1);
}

Output of the above program is

90

The function sample gets evaluated from inside out. In other words the innermost call gets evaluated first followed by next outer function call and so on till the outermost function call.

Here, first innermost function call of sample(a) gets called with value of a as 50 and evaluated and the result namely 60 is returned with which the next outer call sample gets called with value of 60 and returns the value 70 and proceeded till the outermost call is reached and thus value of 90 is returned which gets printed.

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It