function call by value and by reference

M

mitchellpal

i am really having a hard time trying to differentiate the
two..........i mean.....anyone got a better idea how each occurs?
 
V

Vladimir S. Oka

i am really having a hard time trying to differentiate the
two..........i mean.....anyone got a better idea how each occurs?

In C, function call is a function call. There's no calling by
value or reference.

If you meant passing parameters by value and by reference,
you're out of luck as well, as C passes them by value /only/.
That means that a copy of the parameter is passed to the called
function, not some reference to the original in the calling
function.

Now, those textbooks would've helped here, too, you know.

Cheers

Vladimir
 
M

mitchellpal

boy you mastered my email...........but there is this passing of
parameters by reference.....sorry i mistook.... i mean like when you
use pointers... what you got now?
 
E

Eric Sosman

i am really having a hard time trying to differentiate the
two..........i mean.....anyone got a better idea how each occurs?

All C function arguments are passed by value, always.
C has no "call by reference" nor "call by name." You can
simulate the former by passing a pointer to the referenced
object:

void f(int *ptr) {
*ptr = 42;
}
...
int x;
f(&x); /* set x to 42 */

.... but the pointer itself is still passed by value.
 
C

Chris Dollin

i am really having a hard time trying to differentiate the
two..........i mean.....anyone got a better idea how each occurs?

There are books on programming languages which explain these terms.
You should go find & read some.

Roughly: a call f(X) to a function defined by f(a ofType T) is
call-by-reference (better would have been "pass by value", but we'll
let that ... through) if updating the argument a inside f results in
(immediately) updating the variable X. It's call-by-value if changing
the value of a doesn't change the value of X.

C doesn't have call-by-reference. C++ does (when the argument is
defined to have a reference type) as does Pascal (when the argument
is a VAR parameter).

So

void example( T a ) { a = someTValue; }

void caller() { T a = aTValue; example( a ); ... }

in the ... the value of `a` is /still/ aTValue, not someTValue.
Even if T is a pointer type.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

i am really having a hard time trying to differentiate the
two..........i mean.....anyone got a better idea how each occurs?

My two cents (may be devalued by others in the newsgroup :) )

With "call by value", the caller passes the /value/ of the source data
to the target. If the target changes the data it receives, it only
changes a copy of the value, not the original data.

With "call by reference", the caller makes the source data directly
available to the target. Both source and target refer to the data in the
same way, and changes that the target makes to the data will be
reflected in the original data.

C uses the "call by value" semantic, not the "call by reference"
semantic. "call by value" using pointers can obtain similar results as
"call by reference", although the semantic is still "call by value" and
the target function must use special operations to make the effects happen.

*IF* C supported "call by reference", the following would be valid

void target(int number)
{
number = 10;
}

void caller(void)
{
int a_number = 3;

target(a_number);
/* at this point, call by reference
** would leave a_number set to 10
*/
}

However, C only supports "call by value", so the above would not result
in a_number (in caller() ) being set to a value of 10 by target().

In C, you can /emulate/ "call by reference" using pointers. The
emulation of the above code would look like:

void target(int *number)
{
*number = 10;
}

void caller(void)
{
int a_number = 3;

target(&a_number);
/* at this point, emulated call by reference
** leaves a_number set to 10
*/
}

Note the difference: in the "call by reference" example above, both
target() and caller() shared a common definition of the data (int). In
the /emulated/ "call by reference", caller() defines the data as int,
and target() defines it's access as pointer. Not true "call by
reference", but does the same thing.




- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFD0Q82agVFX4UWr64RAj1RAKC80K5Bj8miV+lciUUHiM4waX7k1QCeOs+d
AnCAfnhoyh6UsTU50fh7JnQ=
=gCCs
-----END PGP SIGNATURE-----
 
N

Nelu

boy you mastered my email
who did?
...........but there is this passing of
parameters by reference.....sorry i mistook.... i mean like when you
use pointers... what you got now?
Please quote some context so others can understand what you're saying.

When you have a function that takes pointers as arguments, when
you call that function the value of those pointers is sent to the function.
The value of the pointer is the address of something (the pointer is
a reference to something), but you still pass the value of the address
(pointer). It emulates pass by reference but it's not pass by reference.

For example:
void myfunc(char *ch) {
*ch='c';
}

int main(void) {
char a='b';
myfunc(&a);
return 0;
}

myfunc receives a pointer so myfunc(&a) sends the value of the pointer to a.

in C++ you can write something like this:
void myfunc(char &ch) {
ch='c';
}

int main(void) {
char a='b';
myfunc(a);
return 0;
}

which means myfunc receives a reference. You do ch='c' because
you received a reference to a not a pointer that holds the address of a.
Even if it's likely that behind the syntax, things are going to be
implemented the same (passing a reference by value like in C)
C++ makes the process transparent.
Maybe there are other ways of implementing pass by reference and
in that case even the implementations are different. Not sure if it
can't be
done but it doesn't say anywhere it has to be done in a certain way.
 
V

Vladimir S. Oka

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



My two cents (may be devalued by others in the newsgroup :) )

With "call by value", the caller passes the /value/ of the source data
to the target. If the target changes the data it receives, it only
changes a copy of the value, not the original data.

With "call by reference", the caller makes the source data directly
available to the target. Both source and target refer to the data in the
same way, and changes that the target makes to the data will be
reflected in the original data.

[snipped lots of example code]
Note the difference: in the "call by reference" example above, both
target() and caller() shared a common definition of the data (int). In
the /emulated/ "call by reference", caller() defines the data as int,
and target() defines it's access as pointer. Not true "call by
reference", but does the same thing.

Your explanation is great, but I'd still use "pass" rather than
"call", as parameters are /passed/ to functions, and functions
themselves are /called/. For the latter, "by reference" or "by
value" have no meaning.

Cheers

Vladimir
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1




My two cents (may be devalued by others in the newsgroup :) )
[snip]

Your explanation is great, but I'd still use "pass" rather than "call",
as parameters are /passed/ to functions, and functions themselves are
/called/. For the latter, "by reference" or "by value" have no meaning.

Good point.


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFD0RtuagVFX4UWr64RAkhBAJ9nVgqtPKkKrDxJqzgJ9Q3aTQGduACfZUal
SLrYvnLBsSrs4s5pD5tnKDk=
=VdZE
-----END PGP SIGNATURE-----
 
K

Keith Thompson

Vladimir S. Oka said:
Lew Pitcher wrote: [...]
Note the difference: in the "call by reference" example above, both
target() and caller() shared a common definition of the data (int). In
the /emulated/ "call by reference", caller() defines the data as int,
and target() defines it's access as pointer. Not true "call by
reference", but does the same thing.

Your explanation is great, but I'd still use "pass" rather than
"call", as parameters are /passed/ to functions, and functions
themselves are /called/. For the latter, "by reference" or "by value"
have no meaning.

"Call by foo" (for foo = "value", "reference", or "name") was a common
term in the past (1960s or so?). When the term was used, I think that
most or all languages passed *all* parameters using the same method.
Some more modern languages (but not C) allow the method to be
specified for each parameter. I agree that "pass by foo" is clearer.
 
D

Dik T. Winter

> "Call by foo" (for foo = "value", "reference", or "name") was a common
> term in the past (1960s or so?).

Even earlier, I think.
> When the term was used, I think that
> most or all languages passed *all* parameters using the same method.

Wrong. Algol 60 was a language where the kind of passing (by value or
by name) was specified for each parameter. (Actually it was by name
unless the callee explicitly specified it was by value.) And with
Algol 60 the distinction between the various methods became known.
The other major language at that time (Fortran) said nothing about it.
Later versions specified that (for Fortran) it was either by
reference or by value/return at the discretion of the system, because
no valid program would be able to detect the difference.
 
K

Keith Thompson

Dik T. Winter said:
Even earlier, I think.


Wrong. Algol 60 was a language where the kind of passing (by value or
by name) was specified for each parameter. (Actually it was by name
unless the callee explicitly specified it was by value.)
[...]

Thanks for the correction.
 
D

Dave Thompson

Roughly: a call f(X) to a function defined by f(a ofType T) is
call-by-reference (better would have been "pass by value", but we'll

IrrTYM 'pass by reference' here.
let that ... through) if updating the argument a inside f results in
(immediately) updating the variable X. It's call-by-value if changing
the value of a doesn't change the value of X.

C doesn't have call-by-reference. C++ does (when the argument is
defined to have a reference type) as does Pascal (when the argument
is a VAR parameter).
Pedantic, and only since I'm already replying: when the _parameter_
has reference resp. VAR type. The corresponding _argument_ must be an
lvalue resp. variable, i.e. not just a value.

- David.Thompson1 at worldnet.att.net
 
C

Chris Dollin

Dave said:
IrrTYM 'pass by reference' here.
Yes.

Pedantic, and only since I'm already replying: when the _parameter_
has reference resp. VAR type. The corresponding _argument_ must be an
lvalue resp. variable, i.e. not just a value.

Can I weasel and say "argument" was short for "formal argument"?

OK, OK. Well caught, sir. IOU1PINT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top