Re: What's the difference of those two ways of passing parameters



On 18 Apr 2009 at 9:08, BartC wrote:
And I thought that, while & references seem to be a C++ concept, it
was useful to point out that they might be available on at least some
C implementations, obviously as an extension.

I think it's also useful to have a broader perspective on this sort of
thing, because it helps in understanding some of the conceptual problems
people sometimes have with C.

Parameter passing in C is beautifully simple: everything is passed by
value, and if you need to do what other languages call "pass by
reference", you just pass a pointer and modify the object through that.

In languages like Java, the situation is a mess. Pretty well everything
is passed as a reference, except certain "built in types" like int and
double, which are usually passed by value. But at least you can learn,
for each type, how it will be passed.

C++ is even worse, because if you see code like

int foo = 42;
bar(foo);
assert(foo == 42);

then you have no way of knowing whether the assert will succeed without
looking at the prototype of bar() to find out whether it just takes an
int or a reference to an int. In my opinion, this is horribly confusing,
especially when bar() is some sort of overloaded method in a multiply
inherited class or whatever, and it's hard to find out what the hell is
actually going on.

On the other hand, passing things by constant reference in the public
interface of a class is such a well-established idiom in C++ that it's
hard to avoid running across it.

So it's perfectly natural for people to wonder what the differences in
argument passing are in C compared to other languages, and why shouldn't
they ask about it here? If they want to crosspost to another group, then
that's fine too.

.



Relevant Pages

  • RE: Passing arguements by reference
    ... even without using the ref keyword. ... Passing a value type by value ... Passing a value type by reference ... When you pass value types they by default get copied, so passing an int into ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Another spinoza challenge
    ... and passing the address of a variable by ... It appears that some people can't grasp the fact that one can ... effect pass by reference by passing variables by address. ... int main ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... effect pass by reference by passing variables by address. ... int main ...   byref; ...
    (comp.lang.c)
  • Re: A doubly linked-list in C
    ... yet "reference" can't. ... int foo; ... Are we passing i by reference? ... You are passing something that refers to i. ...
    (comp.lang.c)
  • Re: How to update an agrument passed by name in scheme
    ... Scheme, which, based on this thread, cannot be done trivially. ... passed by reference in Scheme. ... In languages that have first-class references (like ... same effect by passing such a first-class reference value. ...
    (comp.lang.scheme)

Loading