# Thread: This should be an easy one for yall (beginner)

1. ## This should be an easy one for yall (beginner)

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! 2. Code:
```#include <stdio.h>

int foo (int q) // this function is run when you call it, not wherever you write it down
{
int y = 1;
printf("q=%i\n", q);
return (q + y); // if q=5, this will return 5+1
}

int main ()
{
int x = 0;
while (x < 3) { // 0 < 3, so we enter the loop
printf ("x=%i\n", x); // prints 0
x = x + foo(x); // 0 = 0 + foo(0); Now it "jumps" to foo (line #3), then returns here (line #15) with the result
}
}```
printf ("foo=%i", foo) gave me a big ol long number
Functions do not have a value, they compute one, and in your case that's the value you return. What you're printing there is the address of the function, not something to concern yourself with just yet.

is main just the first function executed no matter what?
Yes, always. Main is the entry point into your program. Everything starts there.

here's where I get really lost. so this is executed before foo, why is that?
If your code ran wherever you wrote it down, what would be the use of having a function?

You should really start simpler, for instance by not printing in both foo and the loop. Then you wouldn't have gotten the wrong idea. 3. So is q automatically 0 since we never assigned it a value? and is foo(x) the just the value that foo returns if it used x as the value instead of q?

So when it jumps to foo which prints q=0 and then returns first the value of 1, so now x is 1. back to the top of the loop, x<3 so it prints 1, then goes to foo which will now print q=1 and return 2, so now x=1+2 so now x=3 and the loop stops. Got that now!

Also, is this true?
"(int q) means that our foo function will need an integer variable and we're telling it that variable is q while also simultaneously defining q. could also have been written as just (q) if q was defined earlier (i think?)" 4. Originally Posted by seten
So is q automatically 0 since we never assigned it a value?
No. Since q is a parameter, it is assigned a value every time the function is called, i.e., its value is the corresponding argument passed to the function. Originally Posted by seten
and is foo(x) the just the value that foo returns if it used x as the value instead of q?
foo(x) is an expression that means a call of the function foo with x as the argument. This expression evaluates to a value, i.e., the return value of the call to foo(x). Originally Posted by seten
So when it jumps to foo which prints q=0 and then returns first the value of 1, so now x is 1.
No. The argument x is passed by value, i.e., for that particular function call, q contains a copy of the value of x. Hence, any changes to q from within the function do not affect x in the caller. Originally Posted by seten
Also, is this true?
"(int q) means that our foo function will need an integer variable and we're telling it that variable is q while also simultaneously defining q. could also have been written as just (q) if q was defined earlier (i think?)"
It is true that that is both a declaration and definition of q, but you cannot write it as just q because that is not the syntax of a function parameter*.

* a caveat is that in the past, C did allow you to do this, but it is obsolescent syntax that I shall not discuss further. 5. Originally Posted by -Adrian is main just the first function executed no matter what?
Yes, always. Main is the entry point into your program. Everything starts there.
There are ways to execute functions other than main() beforehand, but this depends on your compiler and it isn't standard. For GCC you can do a little research on function attributes...

For practical reasons, it is safer to think main() is always the "main" entry point of any C program... 6. Originally Posted by laserlight No. The argument x is passed by value, i.e., for that particular function call, q contains a copy of the value of x. Hence, any changes to q from within the function do not affect x in the caller.
I think I understand what youre saying on its own but I must not because I don't see how that contradicts my statement.
Foo returns (q+y) which in this case is (x+y) or (1), and I understand that neither that nor anything we might do within foo would not affect the value of x in the caller (which is main?). but then we go back to where we left off in main where now we can do x=x+foo(x)=0+1=1, so now x = 1.

What do I have wrong? 7. Ah, since you understand that the return value is used to compute the new value of x rather than being x, you have it right. Popular pages Recent additions back, foo, function, int, main 