It just came to my mind that for C programmers I have to add the following remark:

it is not easy to pass arrays (vectors) by value in C, in contrast to - for example - Pascal. Because in C the name of a vector is equivalent to the address of the element of its element zero, this means that, if you pass a vector in C, you pass THE ADDRESS of the first element of the vector,
which in fact implies call by reference.

So, to get a "real" call by value for a vector in C, you have to enclose the vector in a struct definition,
because structs are "really" passed by value.

Something like this:

typedef struct
{
   int x [100];
}
int_vector;

void call_by_value_example (int_vector v)
{
}

int main (void)
{
   int_vector a;
   call_by_value_example (a);   // a is copied during call
}

In Pascal, there is no need to do such "struct embedding"; Pascal arrays will be passed by value, if you specify the array name as a parameter (and if the function prototype specifies call by value; in Pascal this is controlled by the VAR keyword ... with VAR: call by reference; without VAR: call by value). And: Pascal doesn't have a rule similar to C, that the array name is the address of the first element;
an array is an array in Pascal and nothing else.

I mixed this up when I talked about copies of large structures during call by value (in C). (I'm the maintainer of New Stanford Pascal, BTW - New Stanford Pascal has a CONST keyword on parameters, too, which means: copy and call by reference, much the same as dummy arguments
in PL/1 or - as I learned recently - CALL BY CONTENT in COBOL).

Kind regards

Bernd


Am 02.04.2023 um 16:41 schrieb Bernd Oppolzer:
Am 02.04.2023 um 16:29 schrieb Seymour J Metz:
Regardless of the implementation, call by reference is about more than efficiency; sometimes a subroutine is required to alter one of its parameters, and call by value doesn't allow that.

That's well known;
most C textbooks tell that call by value is impractical for large structures or arrays because of the copy involved; that's why for performance reasons often pointers are passed,
although no modification of the parameters passed is desired or required.
That's what I referred to.

Kind regards

Bernd

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to