That's a completely inaccurate statement.

In C, passing an integer variable by value results in the same integer value being assigned to the argument.

Passing an array in Perl by value does not any more end up with the same "array value" inside the sub than passing a bare, anonymous list ends up with the same "list value" inside the sub. In fact, the bare, anonymous list is more likely to be preserved in its entire identity (only because it has less identity to lose) than the array is. All that gets passed are multiple scalars in order.

When passing a pointer to a variable in C, you are not passing anything by reference at all. You are passing a pointer by value, and must use the pointer inside the function as a pointer and not as the variable. This is the same as taking a reference to a variable in Perl and passing that in. The technique of using the prototype feature to force the taking of the reference is syntactic sugar.

An aside... K&R call passing a pointer into a function "pass by pointer", because it is pass-by-value but buys you some of the benefits of pass-by-reference when you really need them. The possibility of using pointers is part of what convinced Dennis M. Ritchie that C should be pass-by-value, because with them pass-by-reference's additional caveats are only necessary when you choose to emulate pass-by-reference and not the rest of the time.

If you really want to see what pass-by-reference is, take a look at the pseudocode below.

foo = 1; bar = 5; function inc_dec( x, y ) { x++; y--; return( x, y ); } res1, res2 = inc_dec( foo, bar );
If this was a pass-by-value language, foo would be 1, bar would be 5, res1 would be 2, and res2 would be 4. If it was pass-by-reference, res1 would still be 2 and res2 would still be 4. Yet with pass-by-reference our imaginary language here would produce foo with a value of 2 and bar would have a value of 4. No dereferencing within the function is necessary. That's pass-by-reference.

Update: s/bar have a value/bar would have a value/


In reply to Re^17: If you believe in Lists in Scalar Context, Clap your Hands by mr_mischief
in thread If you believe in Lists in Scalar Context, Clap your Hands by gone2015

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.