alek
February 26th, 2006, 07:36 PM
Hello all!
I understand this might sound like a very newbie question for all of you but... please bear with me :)
I am interested in the syntactic difference between the following two types of parameter passing
a) pass by address
b) pass by reference
I will first describe my understanding of pass by address, and then ask a question for pass by reference:
Okay, passing by address is straightforward, it is logical and is a pure application of pointers.
So, when we declare a function, we have
void exampleFunction (int *para);
but when we actually pass an argument, say, in main function, we say
{
...
int arg = 6;
exampleFunction (&arg);
...
}
This basically relies on the property that, if the function parameter is a pointer (int *para), when we pass an argument to it, we have to pass a memory address (pointer), and so we pass &arg.
Equivalent in saying: para = &arg;
and then we do whatever we wanted to do in our function.
Like, in order to change the value of arg, we would say:
*para = 500;
NOW, MY QUESTION IS THE FOLLOWING:
when we do the pass by reference (which provides the same effect as the pass by address), we seem to ignore, or, even worse, violate the syntactic meanings surrounding pointers.
In the book that I am using, it says that ampersand operator "'&" returns the memory address of a variable, or, in other words, returns a POINTER to that variable.
How does this make sense, then:
void exampleFunctionTwo(int ¶Two);
and then in main we use it as:
...
int argTwo = 10;
exampleFunctionTwo(argTwo);
...
I mean... ¶Two returns the address of the variable paraTwo... but then, what we are saying is:
¶Two = argTwo; //the process of actually passing it
so, how can we assign an INTEGER VARIABLE to ADDRESS of another INTEGER VARIABLE?
in other words, if &a returns a pointer to a, then, how can we assign an INTEGER VARIABLE to a POINTER?
what is even worse, when we manipulate paraTwo in the body of the function, we use it as a regular variable, totally ignoring the fact that it is a pointer to a variable... using the same example like the one above, if I wanted to change the value of argTwo, rather than typing:
*paraTwo = 500;
we type:
paraTwo = 500;
instead.
I... I am CONFUSED... I get the pointers, and I perfectly see the validity in passing by address, from the syntactical view of pointers...
but... pass by reference, simply does not make sense...
Thanks,
Alek
I understand this might sound like a very newbie question for all of you but... please bear with me :)
I am interested in the syntactic difference between the following two types of parameter passing
a) pass by address
b) pass by reference
I will first describe my understanding of pass by address, and then ask a question for pass by reference:
Okay, passing by address is straightforward, it is logical and is a pure application of pointers.
So, when we declare a function, we have
void exampleFunction (int *para);
but when we actually pass an argument, say, in main function, we say
{
...
int arg = 6;
exampleFunction (&arg);
...
}
This basically relies on the property that, if the function parameter is a pointer (int *para), when we pass an argument to it, we have to pass a memory address (pointer), and so we pass &arg.
Equivalent in saying: para = &arg;
and then we do whatever we wanted to do in our function.
Like, in order to change the value of arg, we would say:
*para = 500;
NOW, MY QUESTION IS THE FOLLOWING:
when we do the pass by reference (which provides the same effect as the pass by address), we seem to ignore, or, even worse, violate the syntactic meanings surrounding pointers.
In the book that I am using, it says that ampersand operator "'&" returns the memory address of a variable, or, in other words, returns a POINTER to that variable.
How does this make sense, then:
void exampleFunctionTwo(int ¶Two);
and then in main we use it as:
...
int argTwo = 10;
exampleFunctionTwo(argTwo);
...
I mean... ¶Two returns the address of the variable paraTwo... but then, what we are saying is:
¶Two = argTwo; //the process of actually passing it
so, how can we assign an INTEGER VARIABLE to ADDRESS of another INTEGER VARIABLE?
in other words, if &a returns a pointer to a, then, how can we assign an INTEGER VARIABLE to a POINTER?
what is even worse, when we manipulate paraTwo in the body of the function, we use it as a regular variable, totally ignoring the fact that it is a pointer to a variable... using the same example like the one above, if I wanted to change the value of argTwo, rather than typing:
*paraTwo = 500;
we type:
paraTwo = 500;
instead.
I... I am CONFUSED... I get the pointers, and I perfectly see the validity in passing by address, from the syntactical view of pointers...
but... pass by reference, simply does not make sense...
Thanks,
Alek