Dealing with function call syntax outside of function -


i apologize if exceptionally elementary, started programming in school, have looked on solution , unfortunately nothing has helped me this. have piece of code:

   #define _crt_secure_no_warnings #include <stdio.h>  int logic(int a, int b) {     int c = % b;     a++;     b--;     printf("==%d %d %d==\n", a, b, c);     return b + + c; }  int main() {     int a, c;     float d, f;     = 10;     c = 5;     f = 2;      d = logic(a, logic(c, f));     printf("%d %d %.2f %.2f\n", a, c, d, f);     return 0; } 

now output is:

'== 6 1 1== ==11 7 2== 10 5 20.00 2.00' 

now problem how line 'd = logic(a, logic(c, f));' compile in regards logic function above. assume first output, logic function takes value of 5 , 2 c , f , runs through function , b. totally stumped why next output '==11 7 2=='. return 'c + b + a;' exactly, when replace + operator comma first value in output (which 11 regardless of order place variables) emerges, rest 0s.

please this, incredibly stumped, have been @ hours , still nothing.

i assume first output, logic function takes value of 5 , 2 c , f , runs through function , b.

that correct. happens @ first equivalent calling logic( 5, 2 ). note 2 things: a) inside function logic have printf prints output screen, , b) function logic returns value of type int. that's first int in int logic(int a, int b) indicates.

now let's @ original call:

d = logic(a, logic(c, f));

this tells machine want value returned function logic , store in in variable d. however, call logic need 2 int parameters. first 1 given variable a, find value second parameter function needs executed first, happens logic in case.

this same in plain math: calculate f(g(x)) you'll have first calculate g(x) , use result input f. in program, use variable store intermediate result, int g_result = g(x); f( g_result ); equivalent.

so, calculate result of logic(a, logic(c, f));, logic needs run twice. every time logic executed runs across printf , produces output on screen.

and that's why 2 lines of output "== ... ==", 1 each run of function logic.

as said above, equivalently write:

int first_result = logic( c, f ); // call produces output of "== 6 1 1==" , returns value 8 (= 6 + 1 + 1) gets assigned "first_result"  d = logic( a, first_result ); // call produces output of "==11 7 2==" function parameters "a" (=10) , "first_result" (=8). 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -