473,809 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1646
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.i nvalidwrote:
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.n etwrote:
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.n etwrites:
[...]
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_Keit h) 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.n etwrites:
[...]
>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.in validwrote 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*******@btin ternet.comwrite s:
"Richard Heathfield" <rj*@see.sig.in validwrote 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_Keit h) 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

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

Similar topics

2
5296
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 time. The transcript so far is reproduced for your amusement below. To summarise: I've put up a Sudoku-solving program called Sudoku.exe. I want to call it in a php script to solve a puzzle and output the solution. It works fine with...
3
46969
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
2311
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
1936
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 document.write(xyz,x) in the function. <html> <head> <script language="JavaScript">
3
4064
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: oIE.Document.Forms("searchform").Item(<name>).Value = <myvalue> But I cannot control a dropdown with an onchange event. I can set the dropdown's value and selectedIndex, but then calling the onChange() or Click() does not do anything. It only seems to fire the onchange if I
35
10802
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
5
3782
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 as to what this means? I am not trying to alter a table. This particular CL merely generates the next voucher number in a sequence. "SQL0204: HRCU030P in HRZNCUSOBJ type *N not found. Cause . . . . . : HRCU030P in HRZNCUSOBJ type *N was...
13
26601
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
12441
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
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
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 we have to send another system

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.