Passing Variables to functions -REEEALY STUCK :\

I think I'll go on a maniacal massacre if I don't find out how these things work. Kidding, although this has been driving me crazy for the past couple days :|. I've posted this question 3 times and still can't seem to get it. To me, it seems pointless and not that big a deal, I suppose, I just don't seem to catch on to it. Here's an example from the book:
1
2
3
4
5
6
7
8
9
10
11
void fn(int nArg)
{
   nArg = 10;
}

int main()//originally was void parent()

{
   int n1 = 0;
   fn(n1);
}


This is what I believe the program is supposed to do:

1. Identify the 2 functions
2. The value of n1 gets taken to fn()
3. nArg is now and forever equal to 0 (until obviously the programmer changes the value by himself, but nevertheless, is equal to 0)
4. Nothing else changes, n1 is still equal to 0, and whatever fn() decides to do with that value is completley up to it (or the programmer).

SOO - in the end of this all
Nothing has changed in int main(), in fn(), the value that was originally 10, has changed to 0.

If this is wrong..can someone, in the EASIEST way possible explain how this whole thing is supposed to work. This has been killing me for days :(

Last edited on
1. main is called.
2. int n1 is defined and initialized to 0.
3. fn is called with n1 as a parameter.
4. int nArg is defined and initialized to a copy of the value in n1.
5. nArg is set to a value of 10.
6. fn is finished and the program jumps back to main, putting nArg out of scope.
(Note that n1 now still has a value of 0, since nArg was just holding a copy of the value)
7. main is finished.

I hope I explained it in a comprehensible way.
Last edited on
Whenever you pass a variable to a function, it creates a copy of that variable. To modify the variable, you have to use either references (&) or pointers (*). Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//reference
void fn(int& nArg)
{
   nArg = 10;
}

//or

//pointer
void fn(int* nArg)
{
   *nArg=10;
}

int main()//originally was void parent()
{
   int n1 = 0;
   fn(n1);//reference
   fn(&n1); //pointer (in this case, & was used to say the address of n1)
}
Last edited on
n1 started out at 0, then got passed to nArg as 0, then fn() changed it to 10, and then returned to main, where n1 is still 0. Because it didn't change n1, it changed nArg, which is also only in the scope of the function anyway. n1 got passed to fn() and it's value was copied to nArg, which got changed to 10...

Make sense?
Last edited on
What do you mean it creates a copy? In the end, nArg is still equal to 10? That's what I don't understand. What can you do with that copy?
Last edited on
It means when you pass a variable to a function, you are only taking it's value. Hence, you can put in, say fn(10). If it modified the variable you passed, would it change 10? No, of course not. And it doesn't change the variable you pass in either.
Each variable is stored somewhere in memory. So when you change a variable you effectively changed what is stored in that memory space. When you make a copy of that variable you effectively make a new variable with a different memory space. The variables may look the same (i.e have the same values but are actually different cos they don't share the same memory space). So if i change one i don't change the other as different variables. Imagine your variable is a person and this function is a machine that clones the person and puts the clone in a new world. Since what function has put in this new world is a copy of the person and not the actual person, whatever happens to the clone in this new world doesn't affect the original person. The clone could implode and that would not affect the original person
By copy, we mean.... It reads the value of n1 and makes the value of nArg the same... nArg is initialized with the value of n1, or whatever was passed to the func.

And maybe this will help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void fn(int nArg) 
{
    cout << nArg << endl;
}

int fn1(int nArg) 
{
    nArg = 10;
    return nArg;
}

int main() 
{
    int n1 = 0;
    fn(n1); // passes n1 to fn() which then prints nArg.
    cout << fn(n1) << endl;    // passes n1 to fn1() and the fn1() returns nArg

    return 0;
}
Last edited on
Okay, so all you're basically doing it taking the value of n1... and giving it to nArg.
But since nArg later changes itself to 10...in the end, everything stays the same.(nArg is still 10, n1 is still 0)
Were it to not change its value back to 10, the value of nArg would equal 0?
Say I added 5 to nArg, which would equal nArg2? nArg2 would equal 5? (0+5=5)
If you did
1
2
3
int fn(int nArg) {
    cout << nArg + 5 << endl;
}

you would get the number 5 printed on the screen.

If the int passed to fn() was 5, then you'd get 10.


What do you mean by nArg changing itself and all that? I think you are over-complicating it...

The function initializes nArg from the parameter passed to it in main(), and if you do "nArg = " in the function, then it assigns nArg a new value.

But the function does not change anything else outside the function... nArg is it's own separate entity, and so is n1, and so is fn(), and so is main(). They're all independent.
Last edited on
I meant when nArg gets initialized to 10.

What confused me was all that mumbo-jumbo crap the author wrote about how the value of nArg gets changed to 0 when passed. And then, at the end, he said the value of nArg returns to 10, which to me was sort of like a big !@#$ you. I didn't understand why. I thought when you pass a variable to a function, you overwrite it's initialized value (in this case nArg=10) with the value passed.

Im going way to far into this, thanks to everyone who helped me understand it though.

Cheers :)
Ah, you mean when nArg gets reassigned to 10... Right?

Just to clearify, though I think you got it, the variable (variable isn't the correct term but ya know) in main (n1) gets passed to the function (fn) who's parameter (nArg) gets initialized as whatever was passed to it (n1). Then anything done to nArg in the function is a reassignment.
Yeah, that's pretty much it. Kinda went into to much detail with this.Thanks again.
Topic archived. No new replies allowed.