Void function (What will this program print out?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-16-2005
viciousv322
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++??
• 11-16-2005
Daved
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.
• 11-16-2005
viciousv322
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..
• 11-16-2005
Daved
>> 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.
• 11-16-2005
viciousv322
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?
• 11-16-2005
Daved
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.
• 11-16-2005
viciousv322
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..?
• 11-16-2005
viciousv322
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?
• 11-16-2005
Enahs
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;
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.
• 11-16-2005
viciousv322
so it would print out 5 21 ??

• 11-16-2005
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()??
• 11-16-2005
Dae
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().
• 11-16-2005
viciousv322
thanks for the thorough explanation...makes real good sense..
• 11-17-2005
7stud
Quote:

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; }```
• 11-21-2005
Dae
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.

Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last