Okay, help me understand whats going on here.
Code:
#include <stdio.h>
int foo (int q) { /* so here we're declaring a function called foo which returns an integer.
(int q) means that our foo function will need a variable and we're telling it that variable is q. could also have been written as just (q) if q was defined earlier (i think?).
Heres my first question - whats the value of q? does it just default to 0 since we never define it as any specific value? */
int y = 1;
printf ("q=%i\n", q);
return (q + y); /* so this means that our function foo will return the value (q+y) which is at this point (0+1) so the value of foo is 1.
how would I get it to print the value of foo?
printf ("foo=%i", foo) gave me a big ol long number and I'm assuming thats because theres a different syntax for printf-ing the value of a function? */
}
int main (){
int x = 0;
while (x < 3) {
printf ("x=%i\n", x);
x = x + foo(x);
/* here's where I get really lost. so this is executed before foo, why is that? is main just the first function executed no matter what?
Now, at the end of this, x is changed to be 0+foo(x). So is foo(x) the value foo returns if its executed with x as the variable instead of q?
In that case it would be the same thing since q and x were both 0, so foo(x) is 1, and x is now 0+1=1. now it loops back to the top and prints q=0.
How does that work? why does it go back to the top of the code instead of continuing the while loop?
Then goes back down to main, sees that x is still less than 3, so prints x=1. now back up to foo(?) and prints q=1.
why did it go back up to foo again and how did q become 1?
Now back down to main and apparently sees that x is no longer less than 3 because it stops and I am THOROUGHLY confused.*/
}
}
Hope I've made my confusion(s) clear. I've got a hunch I'm looking at something in a very wrong way. Thanks in advance!