473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

call by value

mdh
Hi Group,
I know there have been many questions on this topic, but I wish to
clarify this very basic concept.

in:

int main(){
int j;
j = f(8);
.....}

int f(int i){

return i * i;

}

then in the depths of the computer, is this what occurs.

argument of f ie "8" is assigned to int i, a variable unique to f(int
i), which has been created to fulfill the obligations of f(int i), and
is destroyed once i * i is returned. ( which is described as passing a
copy of "8" to the function)

Sorry if this is really rudimentary, but I just saw the light when
arrays are passed as arguments ( I know, not the same as above) and
wanted to go back to the very basics for a moment.

Thanks

Jul 28 '07 #1
21 1602
mdh said:
Hi Group,
I know there have been many questions on this topic, but I wish to
clarify this very basic concept.

in:

int main(){
int j;
j = f(8);
....}

int f(int i){

return i * i;

}

then in the depths of the computer, is this what occurs.

argument of f ie "8" is assigned to int i, a variable unique to f(int
i), which has been created to fulfill the obligations of f(int i), and
is destroyed once i * i is returned. ( which is described as passing a
copy of "8" to the function)
Yes. To be more specific, 8 is ***evaluated***. It turns out, after a
potentially long and tortuous calculation which in this case is
actually probably quite short, to have the value 8. That result is
stored in the i object that is created as part of the process of
invoking the f function. The return statement evaluates the return
expression, i * i, resulting in a value which is then stored in a place
appropriate for return values (quite possibly directly into main's j,
but perhaps via a register or something). Then the function returns
and, as part of the process of returning, the i object is destroyed.
Sorry if this is really rudimentary, but I just saw the light when
arrays are passed as arguments ( I know, not the same as above) and
wanted to go back to the very basics for a moment.
It's exactly the same as above, even with arrays. What you actually pass
is not an array, but an expression involving an array. That expression
is evaluated, and the result of the evaluation is stored in the
parameter object.

The trick is to realise that parameters are objects, but arguments are
expressions. Expressions are evaluated, and the values thus obtained
are stored in parameter objects.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 28 '07 #2
mdh
On Jul 28, 3:51 am, Richard Heathfield :
mdh said:
int main(){
int j;
j = f(8);
....}
int f(int i){
return i * i;
}
Yes. To be more specific, 8 is ***evaluated***.......snip......
>
It's exactly the same as above, even with arrays. What you actually pass
is not an array, but an expression involving an array. That expression
is evaluated, and the result of the evaluation is stored in the
parameter object.

The trick is to realise that parameters are objects, but arguments are
expressions. Expressions are evaluated, and the values thus obtained
are stored in parameter objects.

I like that.
So, in the case of the "array as argument" example, it would be
evaluated, and the result ( the address of the first element) stored
in the Object parameter (of type pointer to Array-type), if I
understand you correctly.

Jul 28 '07 #3
mdh said:
On Jul 28, 3:51 am, Richard Heathfield :
<snip>
>The trick is to realise that parameters are objects, but arguments
are expressions. Expressions are evaluated, and the values thus
obtained are stored in parameter objects.

I like that.
So, in the case of the "array as argument" example, it would be
evaluated, and the result ( the address of the first element) stored
in the Object parameter (of type pointer to Array-type), if I
understand you correctly.
Precisely so, yes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 28 '07 #4
mdh
On Jul 28, 4:03 am, Richard Heathfield <r...@see.sig.invalidwrote:
mdh said:
>
I like that.
So, in the case of the "array as argument" example, it would be
evaluated, and the result ( the address of the first element) stored
in the Object parameter (of type pointer to Array-type), if I
understand you correctly.

Precisely so, yes.
Thank you Richard.

Jul 28 '07 #5
On Jul 28, 11:57 am, mdh <m...@comcast.netwrote:
On Jul 28, 3:51 am, Richard Heathfield :
mdh said:
int main(){
int j;
j = f(8);
....}
int f(int i){
return i * i;
}
Yes. To be more specific, 8 is ***evaluated***.......snip......
It's exactly the same as above, even with arrays. What you actually pass
is not an array, but an expression involving an array. That expression
is evaluated, and the result of the evaluation is stored in the
parameter object.
The trick is to realise that parameters are objects, but arguments are
expressions. Expressions are evaluated, and the values thus obtained
are stored in parameter objects.

I like that.
So, in the case of the "array as argument" example, it would be
evaluated, and the result ( the address of the first element) stored
in the Object parameter (of type pointer to Array-type), if I
understand you correctly.
With arrays, there are two rules that are not very obvious. The first
is what you said, if you have an expression whose result would be an
array, then in most situations that array will be converted to a
pointer to the first element (the exceptions are sizeof (array) which
gives the size of the whole array as you would hope, not the size of a
pointer to the first element, and &array, which gives the address of
the array, not the address of the first element). That rule is used in
the caller of the function.

The other rule is within function definitions: Whenever you define a
function parameter whose type looks like an array of type T, the
compiler automatically replaces this with a parameter of type "pointer
to T". So if you write "int f (int array[100])" then the compiler
automatically replaces it with "int f (int* array)"; both function
declarations behave absolutely identical. So you can't actually have
arrays as parameters. You can write a function declaration that
_looks_ as if you had array parameters, but you actually get a pointer
parameter instead.
Jul 28 '07 #6
mdh <md**@comcast.netwrites:
[...]
So, in the case of the "array as argument" example, it would be
evaluated, and the result ( the address of the first element) stored
in the Object parameter (of type pointer to Array-type), if I
understand you correctly.
Almost. An array expression is converted to a pointer to its first
element, not to a pointer to the array. For example:

int array1[10];
int array2[20][30];

func1(array1);
/* The argument is of type pointer to int. */

func2(array2);
/* The argument is of type pointer to array 30 of int. */

Pointers to arrays actually aren't used very often. In most contexts,
it's more useful to have a pointer to an element of the array, so you
can use it to traverse the elements.

(I presume you've read section 6 of the comp.lang.c FAQ.)

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 28 '07 #7
Keith Thompson said:
mdh <md**@comcast.netwrites:
[...]
>So, in the case of the "array as argument" example, it would be
evaluated, and the result ( the address of the first element) stored
in the Object parameter (of type pointer to Array-type), if I
understand you correctly.

Almost. An array expression is converted to a pointer to its first
element, not to a pointer to the array.
FWIW, I read mdh's "pointer to Array-type" as meaning "pointer to a type
of whatever the heck it is that it's an array of". In other words, if
it's an array of int it becomes pointer to int, if it's an array of
char it becomes pointer to char, etc.

That is, I don't think mdh is confused on this matter, and merely worded
his reply a touch carelessly. Nevertheless, your interpretation is a
reasonable one and therefore your correction is useful.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 28 '07 #8

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:9q******************************@bt.com...
Keith Thompson said:
>Almost. An array expression is converted to a pointer to its first
element, not to a pointer to the array.

FWIW, I read mdh's "pointer to Array-type" as meaning "pointer to a type
of whatever the heck it is that it's an array of". In other words, if
it's an array of int it becomes pointer to int, if it's an array of
char it becomes pointer to char, etc.

That is, I don't think mdh is confused on this matter, and merely worded
his reply a touch carelessly. Nevertheless, your interpretation is a
reasonable one and therefore your correction is useful.
People are going to say "pointer to an array" when they mean "pointer to the
first element of an array". Humans are very intolerant of syntactic bloat.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 28 '07 #9
"Malcolm McLean" <re*******@btinternet.comwrites:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:9q******************************@bt.com...
>Keith Thompson said:
>>Almost. An array expression is converted to a pointer to its first
element, not to a pointer to the array.

FWIW, I read mdh's "pointer to Array-type" as meaning "pointer to a type
of whatever the heck it is that it's an array of". In other words, if
it's an array of int it becomes pointer to int, if it's an array of
char it becomes pointer to char, etc.

That is, I don't think mdh is confused on this matter, and merely worded
his reply a touch carelessly. Nevertheless, your interpretation is a
reasonable one and therefore your correction is useful.
People are going to say "pointer to an array" when they mean "pointer
to the first element of an array". Humans are very intolerant of
syntactic bloat.
Yes, they are, but it's incorrect, because "pointer to an array"
already has a distinct and useful meaning.

The standard uses the term "pointer to a string" to mean a pointer to
the first element of a string. It's able to do this only because a
"pointer to a string" didn't already have a meaning (since a "string"
in C is a data format, not a data type).

If we use the phrase "pointer to an array" to mean a pointer to its
first element, how are we going to talk about actual pointers to
arrays? It's not syntactic bloat at all; it's necessary for
correctness.

If you can come up with an unambiguous shorthand for "pointer to the
first element of an array", I'll consider using it.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 28 '07 #10
In article <ee******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
....
>People are going to say "pointer to an array" when they mean "pointer to the
first element of an array". Humans are very intolerant of syntactic bloat.
Not in *this* ng, they don't.

Jul 28 '07 #11
mdh
On Jul 28, 2:03 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <eeydneu2GKhbNzbbnZ2dnUVZ8sKln...@bt.com>,Malcol m McLean <regniz...@btinternet.comwrote:

...
People are going to say "pointer to an array" when they mean "pointer to the
first element of an array". Humans are very intolerant of syntactic bloat.

Not in *this* ng, they don't.


Thank you all who further replied. I *meant* ptr to the first
element, but appreciate the syntactical corrections...which is why I
really appreciate this ng so much. (I am not sure if the regulars
quite appreciate what a great ng this is, if one treats it with the
respect it deserves.)
Thanks again.

Jul 28 '07 #12
In article <11**********************@m37g2000prh.googlegroups .com>,
mdh <md**@comcast.netwrote:
>On Jul 28, 2:03 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
>In article <eeydneu2GKhbNzbbnZ2dnUVZ8sKln...@bt.com>,Malcol m McLean
<regniz...@btinternet.comwrote:
>>
...
>People are going to say "pointer to an array" when they mean "pointer to the
first element of an array". Humans are very intolerant of syntactic bloat.

Not in *this* ng, they don't.

Thank you all who further replied. I *meant* ptr to the first
element, but appreciate the syntactical corrections...which is why I
really appreciate this ng so much. (I am not sure if the regulars
quite appreciate what a great ng this is, if one treats it with the
respect it deserves.)
You better check. How brown is your nose???

Jul 28 '07 #13
Keith Thompson said:
"Malcolm McLean" <re*******@btinternet.comwrites:
>"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:9q******************************@bt.com...
>>Keith Thompson said:
Almost. An array expression is converted to a pointer to its first
element, not to a pointer to the array.

FWIW, I read mdh's "pointer to Array-type" as meaning "pointer to a
type of whatever the heck it is that it's an array of". In other
words, if it's an array of int it becomes pointer to int, if it's an
array of char it becomes pointer to char, etc.
<snip>
>People are going to say "pointer to an array" when they mean "pointer
to the first element of an array". Humans are very intolerant of
syntactic bloat.

Yes, they are, but it's incorrect, because "pointer to an array"
already has a distinct and useful meaning.
Indeed, but, in mdh's defence, he actually said "pointer to Array-type",
which can be interpreted in the obvious way (i.e. he's wrong) or in a
slightly less obvious way, as outlined above. Presumably he knows which
he meant, and (since he seems to be a bright bunny) will now be aware
of how to make his meaning more precise.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 28 '07 #14
On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:
"Malcolm McLean" <re*******@btinternet.comwrites:
<snip>
People are going to say "pointer to an array" when they mean "pointer
to the first element of an array". Humans are very intolerant of
syntactic bloat.

Yes, they are, but it's incorrect, because "pointer to an array"
already has a distinct and useful meaning.
Distinct, but not all that often useful, perhaps unfortunately.
The standard uses the term "pointer to a string" to mean a pointer to
the first element of a string. It's able to do this only because a
"pointer to a string" didn't already have a meaning (since a "string"
in C is a data format, not a data type).

If we use the phrase "pointer to an array" to mean a pointer to its
first element, how are we going to talk about actual pointers to
arrays? It's not syntactic bloat at all; it's necessary for
correctness.

If you can come up with an unambiguous shorthand for "pointer to the
first element of an array", I'll consider using it.
I've been experimenting for a while with an idea for this, and so far
am only middling pleased, but throw out for consideration:

Given E r [N], * p, (* q) [N]; size_t_or_other_index_type i;
(E is the element type; the array type is 'array of N E', or just
'array of E' when bound does not matter or is not known.)

- p "points INTO" or "is a pointer INTO" array of N E namely r
means p = &r[i] 0 <= i <= N (in the obvious shorthand notation which
is not correct C), or 0 <= i < N in situations where I am definitely
or likely dereferencing the result

- p "points [is a pointer] AT" array of N E r means p = &r[0] = r
For this case I also tried "onto" as a more mathematically traditional
term, but found using it just too pretentious-sounding.

- and for clarity in the rarer case of q == &r, I usually say q
"points [is a pointer] to WHOLE" array of N E r.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Aug 26 '07 #15
David Thompson wrote:
On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:

Do you really think there's a lot of value added in replying to
month-old posts?


Brian
Aug 26 '07 #16
Default User said:
David Thompson wrote:
>On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:


Do you really think there's a lot of value added in replying to
month-old posts?
Yes.

You've been here long enough to know that David Thompson knows his
stuff. But rather than take part in the main discussion, he waits until
the discussion appears to be complete. Then and only then, he picks up
on any significant loose ends that appear to have been left. Thus, he
never bothers to make a point that someone else is going to make anyway
(because he gives them much more than ample opportunity to make it
themselves). This saves on bandwidth, and provides a backstop for loose
ends.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 26 '07 #17
In article <5j*************@mid.individual.net>,
Default User <de***********@yahoo.comwrote:
>David Thompson wrote:
>On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:


Do you really think there's a lot of value added in replying to
month-old posts?
Gosh, you are one obnoxious twit. And I'm not alone in this.

Aug 26 '07 #18
Richard Heathfield wrote:
Default User said:
David Thompson wrote:
On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:

Do you really think there's a lot of value added in replying to
month-old posts?

Yes.

You've been here long enough to know that David Thompson knows his
stuff. But rather than take part in the main discussion, he waits
until the discussion appears to be complete. Then and only then, he
picks up on any significant loose ends that appear to have been left.
Thus, he never bothers to make a point that someone else is going to
make anyway (because he gives them much more than ample opportunity
to make it themselves). This saves on bandwidth, and provides a
backstop for loose ends.

I don't see any value added to this dump of old dead threads. If he
wanted to wait a bit and pick up the loose ends, a week or so
afterwards would have been ample time.

As it is, pretty much all this has dropped from active discussion.

Brian
Aug 26 '07 #19
"Default User" <de***********@yahoo.comwrites:
David Thompson wrote:
>On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:


Do you really think there's a lot of value added in replying to
month-old posts?
Plenty. Certainly a lot more than your addendum.
Aug 26 '07 #20
Richard Heathfield <rj*@see.sig.invalidwrites:
Default User said:
>David Thompson wrote:
>>On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:


Do you really think there's a lot of value added in replying to
month-old posts?

Yes.

You've been here long enough to know that David Thompson knows his
stuff. But rather than take part in the main discussion, he waits until
the discussion appears to be complete. Then and only then, he picks up
on any significant loose ends that appear to have been left. Thus, he
never bothers to make a point that someone else is going to make anyway
(because he gives them much more than ample opportunity to make it
themselves). This saves on bandwidth, and provides a backstop for loose
ends.
And allows Mr Heathfield to confuse the issue with his interminable word
games mid thread.
Aug 26 '07 #21
"Default User" <de***********@yahoo.comwrites:
Richard Heathfield wrote:
>Default User said:
David Thompson wrote:

On Sat, 28 Jul 2007 13:56:40 -0700, Keith Thompson <ks***@mib.org>
wrote:
Do you really think there's a lot of value added in replying to
month-old posts?

Yes.

You've been here long enough to know that David Thompson knows his
stuff. But rather than take part in the main discussion, he waits
until the discussion appears to be complete. Then and only then, he
picks up on any significant loose ends that appear to have been left.
Thus, he never bothers to make a point that someone else is going to
make anyway (because he gives them much more than ample opportunity
to make it themselves). This saves on bandwidth, and provides a
backstop for loose ends.


I don't see any value added to this dump of old dead threads. If he
wanted to wait a bit and pick up the loose ends, a week or so
afterwards would have been ample time.

As it is, pretty much all this has dropped from active discussion.
How petty can you get?

This is a thread in an archived group. He can reply and add to the
knowledge base whenever he sees fit. And, fwiw, Heathfield made a valid
point as to why David Thompson ( not Dr DT from UKC I take it?) might
well choose to wait before adding.

There is no time limit.

And to try and impose one makes you even more pedantic than most
consider you to be already.
>
Brian
--
Aug 26 '07 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Chapman | last post by:
I am at my wit's end trying to get information out of Streamline.net's support dept about my problem. They reply quickly enough, but seem to try and give out the least possible amount of info each...
3
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
0
by: Hubert Baumeister | last post by:
Fifth International Conference on eXtreme Programming and Agile Processes in Software Engineering XP2004 June 6-10, 2004, Garmisch-Partenkirchen, Germany http://www.xp2004.org/
5
by: Mario Thiel | last post by:
Hello, i am new to JavaScript and right now i am learning Call by Value. i tryed to write a small script but somehow it doesnt work right. i cant put <br> into it, and i can only put the...
3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
5
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
0
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...

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.