Question about passing variables/pointers in functions.

My book started talking about pointers. Smooth sailing, however Ive come across this, I've listed everything the author says, if someone can please make this more clearer to me, I've listed my questions below it. Thanks in advance!
----------------------------------------------------------------------------------------------------------------------------------------------
Passing Pointers to Functions

One of the uses of pointer variables is in passing arguments to functions. To understand why this is important, you need to understand how arguments are passed to a function. By default, arguments are passed to functions by value. This has the somewhat surprising result that changing the value of a variable in a function does not normally change its value in the calling function. Consider the following example code segment:

void fn(int nArg)
{
nArg = 10;
// value of nArg at this point is 10
)

void parent(void)
{
int n1 = 0;
fn(n1);
// value of n1 at this point is still 0
}


Here the parent() function initializes the integer variable n1 to 0. The value of n1 is then passed to fn(). Upon entering the function, nArg is equal to 0, the value passed. fn() changes the value of nArg to 10 before returning to parent(). Perhaps surprisingly, upon returning to parent(), the value of n1 is still 0.

The reason for this behavior is that C++ doesn't pass a variable to a function. Instead, C++ passes the value contained in the variable at the time of the call. That is, the expression is evaluated, even if it is just a variable name, and the result is passed.
----------------------------------------------------------------------------------------------------------------------------------------------

*What does he mean they're passed by value? The variable has to be passed to.

*When this happens: fn(n1), he's passing the value to fn()...but...what does that do? What's the outcome? Does the variable n1 and it's value get initialized in fn? And that's it? So basically they're taking the value of n1 to the other function right? Or am I wrong.

*What does he mean by "upon entering the function, nArg is equal to 0, the value passed" When did this happen? So it get's initialized to 0. But then it says "fn() changes the value of nArg to 10 before returning to parent(). When did it return to parent(), when did it even go to parent?!

*So in the end, what happens? What's the outcome of this?


This is making absolute no sense to me. Can someone please clarify this, in simpler terms. I know it's laid out very clearly, however it's the code itself that's confusing me.
Last edited on
It means that when you do fn(n1) in parent, you are basically doing fn(0). Inside fn(), the argument nArg is now 0, completely different and separate from n1 in parent(). That's why it is called passing by value; you are not passing the variable, just the value of the variable (in this case, 0).

Have you studied functions yet? It returns to parent() when you are finished with the fn() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
void fn(int nArg)
{
    nArg = 10;
    // value of nArg at this point is 10
    // return to wherever we were called - in this case, line 12
)

void parent(void)
{
    int n1 = 0; // n1 is now 0
    fn(n1); // do whatever is in fn(), with nArg = 0
    // value of n1 at this point is still 0; it hasn't been changed
}
So all it does is put the value of n1 into nArg (0)? But then it says "fn() changes the value of nArg to 10 before returning to parent"..
When parent() calls fn() the variable n1 is COPIED into variable nArg. The variable nArg is destroyed when function fn() returns. So the value of n1 remains unchanged.
Oh I see. So in the end, the value of nArg is just changed to 0, and everything else stays the same right?
What if it's like this

void fn()
{
nArg=10;
nArg2=15
}

void parent()
{
int n1=5;
fn(n1);
}



So what happens here? The value of nArg is changed to 5 right? Or are both variables changed to 5?
First: n1 is made 5
Second: You get an error because fn() does not take any arguments >_> (I'll assume you meant to pass nArg - in that case, nArg would get 5, since it is taking a value from n1
Third: nArg is changed to 10 by your assignment
Fourth: Your undeclared variable nArg2 is made 15
Then you leave the fn() function, and nArg and nArg2 stop existing.

Note that n1 is still 5
When you pass a variable into a function the variable is copied. The function only changes the copy, NOT the variable itself.
I'll have to practice with it myself. I just don't understand the point of the second statement and then the third statement, if it just changes back to 10..
The function used in the example is obviously pointless.
Topic archived. No new replies allowed.