1. ## Can somone please break this down for me

int
Code:
```#include <stdio.h>
int f(int a, int b);
int main() {
int a = 6, b = 7, c = 8;
b = f(c+2, 3*a-b);
printf("a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}

int f(int a, int b) {
a = a + b;
b = a - b;
printf("a = %d, b = %d\n", a, b);
return 42;
}```
I have a final today and im trying to understand this part?

a=21 b=10
a=6 b=42 c=8

2. Which part exactly?

Get out a bit of paper. Write columns for every variable (be VERY careful as f defines its own local a and b and you have main definitions with exactly the same name but they will be DIFFERENT variables). Run through the program in your head writing down the values of the variables at each line.

Like this:

Code:
```Line No  |  A   |   B   |   C   |   f's A   |   f's B
1          |  6   |   7   |   8    |    ?      |      ?```

3. What exactly do you not understand about the output?

4. What don't you understand?

Jim

5. Im having a hard time understanding how they get 42 for b than the second time they get a=21 and b =10

6. What value is your function returning, where is that value being placed?

Jim

7. Its returning a=21 b=10
a=6 b=42 c=8

This is a practice example from a test review, we have to write it on paper but I just wanted to see how they got the output they did. I can understand the a and c in the first function but idk how they got 42 for b. Or a=21 and b=10

8. What is this function returning?
Code:
```int f(int a, int b) {
a = a + b;
b = a - b;
printf("a = %d, b = %d\n", a, b);
return 42;
}```
Where is that return value placed?
Code:
`b = f(c+2, 3*a-b);`
Jim

9. Ok but how does b go from 42 to 10?

10. It doesn't "go from 42 to 10". They are two separate variables. One is a local variable in main, the other is a parameter to the f() function.

11. It doesn't "go from 42 to 10". They are two separate variables. One is a local variable in main, the other is a parameter to the f() function.

EDIT: do what gardhr says. That's the only way to actually understand what is happening, trace it line by line on paper. Be the compiler and computer. Run this program in your head.

12. The variable b never goes from 42 to 10. This is your reported output:
a=21 b=10
a=6 b=42 c=8
The variable b in the function has a value of 10, after the function returns b has a value of 42 in main. The variable b in the function is not the same variable b that is in the function.

Jim

13. It's all a question of scope.
Code:
```#include <stdio.h>
int f(int a, int b);
int main() {
int a = 6, b = 7, c = 8;
b = f(c+2, 3*a-b);
printf("a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}

int f(int a, int b) {
a = a + b;
b = a - b;
printf("a = %d, b = %d\n", a, b);
return 42;
}```
These b's are completely different variables.
Changes you make to b inside f() do NOT change the value of b in main.
b (in main) changes with the return result of f() - that's all.

Rename them to fred and barney if it'll make you feel better.

14. Oh wow Lol the paper really helped. Thanks fells.

15. Stepping through it in a debugger really helps too!