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

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    3

    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!
    Last edited by seten; 3 Weeks Ago at 03:58 PM.

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    811
    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.
    Last edited by -Adrian; 3 Weeks Ago at 04:59 PM.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,153
    Quote 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.

    Quote 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).

    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    533
    Quote Originally Posted by -Adrian View Post
    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. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    3
    Quote Originally Posted by laserlight View Post

    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,153
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with easy beginner program
    By aosterminal in forum C Programming
    Replies: 8
    Last Post: 09-25-2012, 08:46 PM
  2. Easy beginner's question about pointers
    By kyldn6 in forum C Programming
    Replies: 4
    Last Post: 10-26-2010, 05:50 PM
  3. Howdie yall
    By pizzapie in forum C# Programming
    Replies: 0
    Last Post: 04-09-2005, 06:37 AM
  4. Easy Beginner Question
    By Prog.Patterson in forum Windows Programming
    Replies: 2
    Last Post: 05-16-2002, 12:52 AM
  5. WHATS UP YALL....i want to learn
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 02-23-2002, 04:20 PM

Tags for this Thread