# Exam question help: Take 2

• 05-02-2010
ÉireKarl
Exam question help: Take 2
Ok so I'v been told already how to do this question in another thread which got troll bombed. All I'm asking for is the answer so that I can work out the cinks in my head. Thanks.

b) If a program contains the main function
Code:

```void main()           {  int X=2, Y=11;               func(X,&Y);               printf("%d %d\n",X,Y);           }```
and another function
Code:

```void func(int P, int *Q)             { int X;               X=(*Q);               P=X/3;             (*Q)=P+1;             }```
what two values will the main function print out?

P.s. This is not a homework question, I'm doing revision so don't think I'm trying to get out of learning.:)
• 05-02-2010
Salem
> X=(*Q);
X is 11

> P=X/3;
> (*Q)=P+1;
Can you work out the rest?

Can you see why X is 11 ?
• 05-02-2010
ÉireKarl
Yeah, the answers I got were X=11 and Y=14/3
I understand the X part because it's equal to the pointer of Y right?

But I'm wondering why X doesn't equal 2 after you printf() considering it doesn't have a pointer to change it?
• 05-02-2010
Salem
> But I'm wondering why X doesn't equal 2 after you printf() considering it doesn't have a pointer to change it?
But the X in main does equal 2 (try it)

The X's in each function are separate variables - changing one does not change the other.
• 05-02-2010
ÉireKarl
Quote:

Originally Posted by Salem
But the X in main does equal 2 (try it)

The X's in each function are separate variables - changing one does not change the other.

Oh right sorry, I just thought you meant X=11 when you printf(). In the second function, are they just making it int X; to confuse me or does it have to be int X; ?
• 05-02-2010
They name it X just because they want to, and the name is available. Unless there is a pointer involved, each function encapsulates (surrounds to make or keep it unique), it's own variables.
• 05-02-2010
ÉireKarl
Oh ok, and Y does equal 14/3?
And how would I check this using devc++? Sorry I have the lowest of understanding when it comes to this subject.
• 05-02-2010
There is no quantity like 14/3 because these are integers -- whole units only.

They're like pennies - nobody ever gives you 14/3 pennies. :)

To check it out, run the program - don't be afraid to investigate these things, yourself. Some exploring is not only GOOD, it's the only way to really learn the subject, well.
• 05-02-2010
ÉireKarl
Quote:

There is no quantity like 14/3 because these are integers -- whole units only.

They're like pennies - nobody ever gives you 14/3 pennies. :)

To check it out, run the program - don't be afraid to investigate these things, yourself. Some exploring is not only GOOD, it's the only way to really learn the subject, well.

Damn I forgot that, first thing they thought us too.I tried running it, don't know what's wrong with it. This is what I'm doing:
Code:

```#include <stdio.h> void main()           {  int X=2, Y=11;               func(X,&Y);               printf("%d %d\n",X,Y);           }            void func(int P, int *Q)             { int X;               X=(*Q);               P=X/3;             (*Q)=P+1;             }```
Please be gentle, I'v only just properly started learning it.;)
• 05-02-2010
Quote:

Originally Posted by ÉireKarl
Damn I forgot that, first thing they thought us too.I tried running it, don't know what's wrong with it. This is what I'm doing:

In your code editor, set your options to use spaces, instead of tabs, and use 2 to 5 (I like 2 for almost everything. Forum software doesn't handle tabs very well.

Code:

```#include <stdio.h> void func(int P, int *Q);  //function prototype int main()            //void main is NOT standard C. I know books have it sometimes {                      //but that's just being lazy and wrong on their part   int X=2, Y=11;   func(X,&Y);   printf("%d %d\n",X,Y);   return 0; }            void func(int P, int *Q) {   int X;   X=(*Q);   P=X/3;   (*Q)=P+1; }```
Please be gentle, I'v only just properly started learning it.;)

Oh don't worry - even if I'm cussing you out, I'm always cussing you out, GENTLY - makes all the difference, I know. :p :p < ROFL! >

Try that - I haven't.
• 05-02-2010
claudiu
Quote:

Originally Posted by ÉireKarl
Damn I forgot that, first thing they thought us too.I tried running it, don't know what's wrong with it. This is what I'm doing:
Code:

```#include <stdio.h> void main()           {  int X=2, Y=11;               func(X,&Y);               printf("%d %d\n",X,Y);           }            void func(int P, int *Q)             { int X;               X=(*Q);               P=X/3;             (*Q)=P+1;             }```
Please be gentle, I'v only just properly started learning it.;)

A few things:

1) look at Salem's user picture to have a laugh over why void main is bad. You should always use int main() and return 0 at the end of your main function.

2) The problem is that you are declaring and implementing void func() after main so main() has no idea what func() is.

Either move the function above main, or type:

void func(int P, int *Q);

above main. This basically tells the main function : "hey, if you see a function called func, this is what it should look like, I have it implemented somewhere else".
• 05-02-2010
ÉireKarl
That's great, thanks guys.