Another C++ Help Thread

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Im new to programming and I was curious as to why this program isnt working, I want it to originally set x = 1, then be able to call the other function which changes x = 3, and returns it to the main function which prints 3, but instead it only prints 1.

#include "stdafx.h"
#include <iostream.h>

int x; //declaring the x variable

int test(int x){ //test function which changes x = 3
x=3;
return x;
}

void main(){
x=1;
test(x);
cout << x << &quot;\n&quot;;
}
 

nd

Golden Member
Oct 9, 1999
1,690
0
0
Well, this one is pretty easy. Calling test(x) is only gonna modify the variable 'x' within the scope of function test().

Think of it this way, when test(x) is called, it's copying the value of variable x into the value of parameter x of function test().. so it's modifying the copied variable, not the original.

To fix this, you need to pass the parameter int x by pointer or reference (&quot;*&quot; or &quot;&amp;&quot;).
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Oh i get it, i tried what you said and it worked with using a reference (&amp;) but not a pointer, the compiler gave me all these errors with a pointer.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Well, lets start off w/ the basics:

You're defining variable 'x' w/ file scope (i.e. global to everything in the file), initializing that variable to the integral constant 1 in main() (which, per the ansi standard, should be defined as returning int, not void), calling the function test and passing the _value_ of the global variable x (which is 1, since you initialized it to 1 in main()) as an argument, changing that argument in test() to x, then returning it to the calling function, which is main() in this case. There's quite a few problems here. You're passing the _value of_ x to test(), which means _all_ modifications to x in the scope of test() will be _local_ to that function only. 'x' in the context of test() is in no way associated to the 'x' in the context of the file. You could have also returned the modified value of 'x' to the calling function and handled it appropriately, which you tried to do. Trouble is, you didn't do anything w/ the returning value. You need to do either of these things:

void test(int n)
{
x = n; // this will set the variable 'x' w/ file scope to the value passed to 'n'
}

int main()
{
x = 1;
test(3); // x will now be 3 (bad way to do things)
}

or

void test(int *n)
{
*n = 3; // this will set the variable pointed to by 'n' to 3
}

int main()
{
x = 1;
test(&amp;x); // x will now be 3
}

or

int test(int n)
{
return n;
}

int main()
{
x = 1;
x = test(3); // x will now be 3
}

Well, there you go. All of these are rather pointless, but since you're just trying to understand the ways of things, I hope this helps.

I just typed all of the above here, so, please disregard any 'random functionality' in the above code.
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Thanks. Ive also got another question. Lets say I include the math header file <math.h> and I want to use the function Math.PI which produces the value of PI. I tried to do it and couldnt get it to work, I could never get anything to work with any class I tried to use, like CString. I tried to use the CString.Left() function to extract characters from a string and it always gave me errors. Can anyone give me a code sample of how to use this? Thanks.