473,471 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

function call by value and by reference

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
13 26567
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
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
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


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
-----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
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
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
-----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
"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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
17
by: ex_ottoyuhr | last post by:
I'm trying to create a function that can take arguments, say, foo and bar, and modify the original copies of foo and bar as well as its local versions -- the equivalent of C++ funct(&foo, &bar). ...
17
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.