By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

function call by value and by reference

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

Jan 20 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
mi*********@gmail.com wrote:
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
--
My e-mail address is real, and I read it.
Jan 20 '06 #2

P: n/a
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?

Jan 20 '06 #3

P: n/a
mi*********@gmail.com wrote:
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.

--
Chris "understanding is a three-edged sword" Dollin
Jan 20 '06 #4

P: n/a


mi*********@gmail.com wrote On 01/20/06 11:07,:
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.

--
Er*********@sun.com

Jan 20 '06 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mi*********@gmail.com wrote:
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+lciUUHiM 4waX7k1QCeOs+d
AnCAfnhoyh6UsTU50fh7JnQ=
=gCCs
-----END PGP SIGNATURE-----
Jan 20 '06 #6

P: n/a
mi*********@gmail.com wrote:
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.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jan 20 '06 #7

P: n/a
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mi*********@gmail.com wrote:
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.


[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
--
My e-mail address is real, and I read it.
Jan 20 '06 #8

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mi*********@gmail.com wrote:
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 :-) )

[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)

iD8DBQFD0RtuagVFX4UWr64RAkhBAJ9nVgqtPKkKrDxJqzgJ9Q 3aTQGduACfZUal
SLrYvnLBsSrs4s5pD5tnKDk=
=VdZE
-----END PGP SIGNATURE-----
Jan 20 '06 #9

P: n/a
"Vladimir S. Oka" <no****@btinternet.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 20 '06 #10

P: n/a
In article <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
....
"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.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jan 21 '06 #11

P: n/a
"Dik T. Winter" <Di********@cwi.nl> writes:
In article <ln************@nuthaus.mib.org> Keith Thompson
<ks***@mib.org> writes:
...
> "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.)

[...]

Thanks for the correction.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 21 '06 #12

P: n/a
On Fri, 20 Jan 2006 16:23:55 +0000, Chris Dollin <ke**@hpl.hp.com>
wrote:
<snip>
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
Feb 6 '06 #13

P: n/a
Dave Thompson wrote:
On Fri, 20 Jan 2006 16:23:55 +0000, Chris Dollin <ke**@hpl.hp.com>
wrote:
<snip>
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.


Yes.
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.


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

OK, OK. Well caught, sir. IOU1PINT.

--
Chris "understanding is a three-edged sword" Dollin
Feb 6 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.