Void function (What will this program print out?

This is a discussion on Void function (What will this program print out? within the C++ Programming forums, part of the General Programming Boards category; Code: void addOne(); // function prototype int x = 10; int y = 20; int main() { int x = ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    37

    Void function (What will this program print out?

    Code:
    void addOne();  // function prototype
    int x = 10;
    int y = 20;
    
    int main()
    {
    int x = 5;
    addOne();	
    printf ("x=%d, y=%d", x, y);
    }
    
    void addOne()
    {
    x++;
    y++;
    }
    This was given to me and i'm supposed to figure out what gets printed out...
    I've been trying to decipher this code... to me it looks like the variables are set at 10 and 20...
    but x=5 would be static and y=20 from the addOne() ??

    How does the counter work x++ and y++??

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You need to figure out how many different variables exist in the program. Then you need to figure out which variables (if any) are affected by addOne, and which variables are used by the printf statement. Then, follow the flow of control and determine their values when the printf statement is reached.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    umm...i didn't learn anything from your post...
    I see two variables... x and y
    but x is defined twice...not sure what the means??
    My logic (very minimal knowledge of C) tells me that x = 5 and y= 20
    i don't know what the counter means

    I need to know what would print out... I've been trying to look it up but can't figure it out...
    thanks again daved...you've been a huge help..

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> but x is defined twice...not sure what the means??

    That means that there are two different variables named x.

    For each line of code starting from the beginning of main (which is the start of the program), write down the value of each variable. Make sure to step into the addOne function and modify the appropriate variables when that code is executed. Feel free to post step by step what you find. Once you get to the printf call, you have values for each variable, and so you know what will be printed. If you post your logic, then we can help you identify what you are missing, but hopefully nobody will just tell you the answer straight away.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    ok..so 3 variables.. 2 x's and a y value

    so it starts the prog by defining x as 5
    but what does it do when it hits the andOne() ??

    when you say that there are two x's...how do i know which one it will print?
    Since andOne() is directly above it... will it change the x value to 10 and keep y at 20? Therfore printing out 10 20
    or does it keep the x=5 and print out 5 20?

    I still have no idea where the increment code comes into play... does it keep looping? Does it loop at all?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Unfortunately, these are all basic tenants of C/C++. If you understand variable scoping, you know which x will print out. If you understand program flow, you know what addOne will do. Combine the two and you know which x variable is affected by addOne.

    The increment operator (++) just adds one to the current value, so if you have a variable a = 3, and then you do a++, a becomes 4. There are no loops.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    so where could i get such info? I have to turn in this problem in an hour and a half... I tried searching variable scoping but couldn't find anything... I'm sorry if i was unclear... I know that the a++ would raise the number up by one...but i don't know what the purpose of it is in the program... (would it affect what gets printed out, because there is no printf after it..?

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    we did a similar problem in class but there was a call function where the increment affected something... does it mean that it would count up from 10 and 20 for 5 times...?

    Code:
    10   21
    11   22
    12   23
    13   24
    14   25
    Am i going in the right direction?

    here is what we did in class...
    Code:
    #include <stdio.h>
    void DisplayNumbers();  // function prototype
    
    int main()
    {
        int i;
    
        for (i=0; i<10; i++)    // call function 10 times
            DisplayNumbers();
    
    	return 0;
    }
    ////////////////////////////////////////
    void DisplayNumbers()
    {
        int x=0;    // new copy of x each call to function
                    // re-initializing to zero each time it is called
        static int y=0; // same copy of y each call to function
                    // using the value from the last call
    
        printf ("x=%d    ", x);
        printf ("y=%d\n\n", y);
    
        x++;    // increment values of x and y
        y++;
    }
    but y is static..what is the difference?

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    void addOne();  // function prototype
    int x = 10;
    int y = 20;
    These values of x and y are globally defined. That means any part of the program can access it.

    int main()
    {
    int x = 5;
    addOne();
    printf ("x=%d, y=%d", x, y);

    }

    x is defined in main, that means that this x can only be accessed by everything in the main brackets ( { everything in here} ). But main can also access the globally defined x.

    The function addOne() is called. This increments x and y by one. Which x ? Well, as I said earlier that the x in main can only be accessed in main, so the addOne function increases the global x by one.

    You only have one y, so it should clearly be 21.

    Which x is outputted? 5 or 11? It is more complicated than this but for now you can think about the brackets as whatever is in the brackets gets more priority to whatever is outside of the brackets. So it will output the x inside the brackets (the one in the main brackets, 5).


    Also, you can use the code and make the program and see the output, and follow it through line by line with the debugger.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    so it would print out 5 21 ??

    Where could i go to learn more about this stuff? I just found this site today...so i will be going through the forums...thanks..

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    so how come it doesn't increment the 5...or does it...
    is it because of the position of the int x=5 (above andOne () ) ?

    Would it change if it were below the andOne()??

  12. #12
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by viciousv322
    so how come it doesn't increment the 5...or does it...
    is it because of the position of the int x=5 (above andOne () ) ?

    Would it change if it were below the andOne()??
    Enahs said..

    It wouldn't change if it were above or below addOne() because the addOne() function does not touch the int x = 5 variable. The reason it increments the int x = 10 variable and not the int x = 5 variable is because int x = 5 is out of scope. By scope it means that int x = 5 only exists in the main() function, whereas int x = 10 exists everywhere because its outside of all functions (its global).

    So that means int x = 10 gets incremented, and if you did not have that x variable in the main() function it would print 11 21. But because you have that x variable in main() anything you call for x inside of main() will use that new one (int x = 5), its sort of overridding that x variable thats outside main() while you're inside main().

    So int x = 5 doesnt get incremented because the addOne() doesn't see int x = 5, it sees int x = 10, so it increments that one. And main() sees int x = 5 and prints that one, which has not been incremented by addOne().
    Last edited by Dae; 11-16-2005 at 08:22 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    37
    thanks for the thorough explanation...makes real good sense..

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    x inside of main() will use that new one (int x = 5), its sort of overridding that x variable thats outside main()
    It's called hiding. Any variable name in a scope(e.g. the code between a pair of brackets) will hide a variable with the same name in a surrounding scope.

    viciousv322,

    What what will this program display?
    Code:
    #include<iotream>
    using namespace stb;
    
    #include<iostream>
    using namespace std;
    
    int x = 10;
    
    void myFunc(int x)
    {
    	cout<<x<<endl;
    }
    
    
    int main()
    {
    	int x = 30;
    
    	for(int i= 0; i<10; i++)
    	{
    		int x = i;
    		cout<<x<<endl;
    	}
    
    	{
    		int x = 50;
    		cout<<x<<endl;
    	}
    
    	cout<<x<<endl;
    
    	myFunc(x);
    
    	return 0;
    }
    Last edited by 7stud; 11-17-2005 at 12:34 AM.

  15. #15
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    It's called hiding. Any variable name in a scope(e.g. the code between a pair of brackets) will hide a variable with the same name in a surrounding scope.
    Much better explanation 7stud. I was just wondering though, isn't that called shadowing? Having more to do with the scope, and if we got into OO it would be a more appropriate term than hiding. Is there any one glossary that could be used as a standard? becuase usually one of the two: overriding, hiding, and shadowing are used to describe situations that are the same if you look at different places. That could be because they are similar concepts, but similar is not the same.

    I've seen overriding more commonly used to refer to methods than members of the class, but I've seen it used for both. I've seen hiding used to refer specifically to members of the class, or when a method that is overloaded has been overridden but not completely (some of the methods become hidden). I'm still very new to programming so I haven't seen many of these definitions used other than in articles and some ebooks.

    Hmm, the fun of programming lingo... could be defined as small, no wait how about low, erm.. actually lets say minimal.

    thorough explanation
    I tend to do that. I'm going to start putting an advisory on my posts letting people know I usually just repeat what I say a few times in different ways for clarity (I hope), and to skip the rest of my blabber to save some time.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21