473,769 Members | 3,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call by value in C

C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?

Feb 19 '06 #1
8 4268
pr************* @yahoo.co.in schrieb:
C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.
The latter is a misconception; C++ can use both.
Ask in comp.lang.c++ about call by value vs. call by reference.

How is call by value advantageous and how is it
implemented?


You can easier implement it as there are no conceptual problems
like "null-references"; how this is done is implementation
specific. It may be copying the values of the first N arguments
to certain registers and the rest into the stack. This has
nothing to do with standard C as it is discussed here, though.

Ask in a platform or implementation specific newsgroup to find
out how it is implemented.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 19 '06 #2
pr************* @yahoo.co.in wrote:
C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?


http://clc-wiki.net/wiki/Pass_by_value
http://clc-wiki.net/wiki/Pass_by_reference
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
Feb 19 '06 #3
pr************* @yahoo.co.in wrote:
C uses call by value for passing of parameters in contrast to C++
which uses call by reference.How is call by value advantageous and
how is it implemented?

So, C++ has call by value and reference. Of course, a C++ programmer can
also 'fake' call by reference - as C does - using a pointer [the pointer is
passed by value, but by dereferencing it, one can access the original
'object'].

How it's done is not specified, but obviously, some 'copy' of actual
parameters is required.

Advantages: one obvious one is that a called function can only
alter/work-with 'copies' of any actual parameters passed to it - so, the
function can go crazy within its own little world [it's a bit like a self
contained program], but cannot affect the caller's original 'objects'.

An obvious disadvantage is that creating copies takes some amount of time -
usually very little of course. However, if a routine is called very many
times a second, this could affect performance. To help here, C has the const
keyword, e.g., char const * const c.

--
==============
Not a pedant
==============
Feb 19 '06 #4
<pr************ *@yahoo.co.in> wrote

C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?

Say we've got this code.

double distsquared = 100000000;
double dist;
double force;
double radius = 8000.0;

dist = sqrt(distsquare d);
if(dist > radius)
force = 1 / distsquared;

perfectly unexceptional code. We are calculating the force between two
planets, which is the reciprocal of the square of distance. However if the
distance is less than the radius, then the calculation breaks down.

Now the code works because we know that the variable distsquared hasn't been
changed by the call to sqrt().

If we had a call by reference semantic, we wouldn't know this.
distsquared could be prserved with its original value, it could be set to
the square root, it could be corrupted or set to zero. We don't know, so our
finger is in the back of the manual, looking for the convention the writer
of sqrt() has chosen.

In call be reference the fucntion sqrt() can "see" distsquared, and work on
it directly.
It is however harder to implement call by value.

The normal way is to maintain a "stack".
So when sqrt() is called, the value of distsquared is pushed onto the stack.
The sqrt() function can then corrupt it to its heart's content. When control
returns, the stack is popped, and the copy discarded.

Incidentally your C++ comment is only partly correct. See the comp.lang.c++
faq for some more infomration about C++ parameter passing.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $6.90 paper, available www.lulu.com
Feb 19 '06 #5
"pemo" <us***********@ gmail.com> wrote

An obvious disadvantage is that creating copies takes some amount of
time - usually very little of course. However, if a routine is called
very many times a second, this could affect performance. To help here, C
has the const keyword, e.g., char const * const c.

const doesn't really help with this.
The pointer is still copied, but the data it points to cannot be modified.
const is really there to help the human programmer know what he may and may
not modify, not to help the compiler optimise.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $6.90 paper, available www.lulu.com
Feb 19 '06 #6
On 2006-02-19, Malcolm <re*******@btin ternet.com> wrote:
"pemo" <us***********@ gmail.com> wrote

An obvious disadvantage is that creating copies takes some amount of
time - usually very little of course. However, if a routine is called
very many times a second, this could affect performance. To help here, C
has the const keyword, e.g., char const * const c.

const doesn't really help with this.
The pointer is still copied, but the data it points to cannot be modified.
const is really there to help the human programmer know what he may and may
not modify, not to help the compiler optimise.


If the data it points to can not be modified then surely it is a
fallacy to deny that this can be used to optimise the compiler. Unless
of course you are silly enough to cast an assignment of the CONST
pointer to a non const.
--
Remove evomer to reply
Feb 19 '06 #7
>> "pemo" <us***********@ gmail.com> wrote
An obvious disadvantage is that creating copies takes some amount of
time - usually very little of course. However, if a routine is called
very many times a second, this could affect performance. To help here, C
has the const keyword, e.g., char const * const c.
On 2006-02-19, Malcolm <re*******@btin ternet.com> wrote:
const doesn't really help with this.
The pointer is still copied, but the data it points to cannot be modified.
const is really there to help the human programmer know what he may and may
not modify, not to help the compiler optimise.

In article <45************ @individual.net >,
Richard G. Riley <rg***********@ gmail.com> wrote:If the data it points to can not be modified then surely it is a
fallacy to deny that this can be used to optimise the compiler.
The problem lies in the "if" part.
Unless of course you are silly enough to cast an assignment of the CONST
pointer to a non const.


This is not where the trouble occurs. Consider the following C
code fragment:

int a[100];

void f(const int *p, size_t n) {
size_t i;

for (i = 1; i < n; i++)
a[i] += p[i - 1];
}

This adds each p[i-1] to a[i], 1 <= i < n. The items to which p
points are marked "const", so f() cannot change them. But they
can change!

int g(void) {
...
f(a, 100);
...
}

An optimizing compiler might "want" to preload the elements in p[],
to make this code go faster. If we assume that the elements in
p[] do not change, this optimization is valid. But p[i-1] names
the same array element as a[i-1], so each modification to a[i]
modifies the value that will be summed next. The desired optimization
is *not* valid, despite the "const".

This is why C99 has "restrict", which actually means what most
people think they want when they write "const".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 19 '06 #8
On 2006-02-19, Chris Torek <no****@torek.n et> wrote:
This is not where the trouble occurs. Consider the following C
code fragment:

int a[100];

void f(const int *p, size_t n) {
size_t i;

for (i = 1; i < n; i++)
a[i] += p[i - 1];
}

int g(void) {
...
f(a, 100);
...
}

This is why C99 has "restrict", which actually means what most
people think they want when they write "const".


Yuck! In this example you are, of course, right :)

--
Remove evomer to reply
Feb 20 '06 #9

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

Similar topics

3
4055
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
10792
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
5
3780
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
26596
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?
1
1524
by: cheryl | last post by:
I make the value of in and out as global that is being input by the user in link1.php. This value should compare to the database before storing. If there are no equal value in the database it will go to link2.php. In this page, the user will input value through option button and then click ok. After that ,user must input his name and age in the link3.php. In this page link 3.php, I want all the value being input from link 1 to link 3 will be...
0
3413
by: news.microsoft.com | last post by:
Hello- I'm having a dickens of a time trying to set the value on a DataGridViewComboBoxColumn. I actually have two DataGridViewComboBoxColumns in my datagrid view. First, I'm setting up the grid/column as follows.. DataGridViewComboBoxColumn someColumn = new DataGridViewComboBoxColumn();
275
12381
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'
7
2153
by: smartic | last post by:
I need to use this function : mb_internal_encoding("UTF-8"); Into class to use it for every mb_ functions into other classes extends from the main class put it give me this error syntax error, unexpected T_STRING, expecting T_FUNCTION that is my code example : class common{ mb_internal_encoding("UTF-8"); } class create extends common{
1
1181
by: techbytes | last post by:
hai, I have three dropdowns.In that ,if i select first dropdown it should populate second drop down and some details should be displayed below the dropdown. I have given two divs in that page.On ajax call,value should be loaded in a single ajax call in two divs. how to do so? regards and thanks in advance
0
9579
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
9422
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
10206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
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
6662
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.