0

I apologize if this is exceptionally elementary, but I just started programming in school, I have looked all over for the solution and unfortunately nothing has helped me with this. I have this piece of code:

   #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int logic(int a, int b)
{
    int c = a % b;
    a++;
    b--;
    printf("==%d %d %d==\n", a, b, c);
    return b + a + c;
}

int main()
{
    int a, c;
    float d, f;
    a = 10;
    c = 5;
    f = 2;

    d = logic(a, logic(c, f));
    printf("%d %d %.2f %.2f\n", a, c, d, f);
    return 0;
}

Now the output for this is:

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

Now the problem is how does the line 'd = logic(a, logic(c, f));' compile in regards to the logic function above. I assume to get the first output, the logic function takes the value of 5 and 2 for c and f and runs it through the function for a and b. But I am totally stumped as to why the next output is '==11 7 2=='. What does return 'c + b + a;' do exactly, when I replace the + operator with a comma only the first value in the output (which is 11 regardless of what order I place the variables) emerges, the rest are 0s.

Please help with this, I am incredibly stumped, have been at it for hours and still nothing.

Impugnor
  • 1
  • 2
  • I believe there's a missing piece of code. Might you please add it and better clarify where you don't get the understanding? – Paolo Maresca Oct 22 '15 at 15:32
  • Nope thats everything. That exactly has the output of what is stated above. I am trying to understand how the first output is 6 1 1, yet the second output is 11 7 2, I do not understand how the second output occurs. – Impugnor Oct 22 '15 at 15:36
  • Note that we're dealing with *two* sets of local variables here. The parameters of `logic()` are named `a` and `b`, but these names are *local* to `logic()` and have nothing to do with `a` and `b` declared in `main()`. – JimmyB Oct 22 '15 at 16:01
  • Okay... So care to explain, if the local values for main differ from the logic values, how does the output arrive with such answers. – Impugnor Oct 22 '15 at 16:05
  • Also note that the *output* comes from the `printf(...)` lines only. The `return b + a + c;` calculates the sum of b, a, and c, and returns that sum's value as the result of the function for further computing. So, to alter the appearance of the *output* on the screen, you have to change the `printf` lines, which is completely independent of the order in which the sum is calculated. – JimmyB Oct 22 '15 at 16:06

1 Answers1

0

I assume to get the first output, the logic function takes the value of 5 and 2 for c and f and runs it through the function for a and b.

That is correct. What happens at first is equivalent to calling logic( 5, 2 ). Note two things: a) Inside the function logic we have a printf which prints some output to the screen, and b) the function logic returns a value of type int. That's what the first int in int logic(int a, int b) indicates.

Now let's look at the original call:

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

This tells the machine that we want to get the value returned by the function logic and store in in variable d. However, to call logic we need two int parameters. The first one is given as variable a, but to find the value for the second parameter a function needs to be executed first, which happens to be logic in this case.

This is just the same as in plain math: To calculate f(g(x)) you'll have to first calculate g(x) and use the result as input for f. In a program, you could as well use another variable to store the intermediate result, like int g_result = g(x); f( g_result ); This is completely equivalent.

So, to calculate the result of logic(a, logic(c, f));, logic needs to run twice. Every time logic is executed it runs across the printf and produces some output on the screen.

And that's why we get two lines of output "== ... ==", one for each run of the function logic.

As said above, you could just equivalently write:

int first_result = logic( c, f ); // This call produces the output of "== 6 1 1==" and returns the value 8 (= 6 + 1 + 1) which gets assigned to "first_result"

d = logic( a, first_result ); // This call produces the output of "==11 7 2==" from the function parameters "a" (=10) and "first_result" (=8).
JimmyB
  • 12,101
  • 2
  • 28
  • 44