Pass by reference or <RETURN>?

Post Reply
Mr. Bigglesworth
Posts: 56
Joined: Fri Feb 16, 2001 6:54 pm

Pass by reference or <RETURN>?

Post by Mr. Bigglesworth »

Which of these function definitions do you guys prefer?

This one, using pass-by-reference:

<a name=createArrayFromFile array_name file_name starting_text ending_text>

<.....>

<$array_name = $array_name $something_else>

</a>

<!--function useage below-->
<createArrayFromFile array_name=$&myArray file_name="hello.txt" starting_text="<***" ending_text="***>">

<loop $myArray>
$myArray<BR>
</loop>

-------------------------------------------------------
Or this, with a <RETURN> value:


<a name=createArrayFromFile file_name starting_text ending_text>

<.....>
<loop....>
<$array_value = $array_value $something_else>
</loop>
<RETURN $array_value>

</a>



<createArrayFromFile file_name="hello.txt" starting_text="<***" ending_text="***>">

<$myArray = $ret>

<loop $myArray>
$myArray<BR>
</loop>



My preference is the first method, with pass-by-reference, since I don't have to do a $ret after the function call, but if that could cause me trouble down the line for whatever reason I'm more than happy to use the <RETURN> version instead.
User avatar
Kai
Site Admin
Posts: 1272
Joined: Tue Apr 25, 2000 1:27 pm

Pass by reference or <RETURN>?

Post by Kai »

Either is fine. I lean towards <RETURN> for single-value-returning functions, but that's mainly personal preference. Since pass-by-reference is determined by the caller not the function, both calls make clear what is being modified/returned, to a user unfamiliar with your function. And with Vortex's copy-on-write storage of variables, there's no real performance difference between the two either.

Whichever style you choose, I recommend using <LOCAL>s if needed to avoid unintended scratching of global vars. Eg. in your <RETURN> example, <$array_value> should be declared <LOCAL> in <createArrayFromFile>, so that it doesn't modify anything other than its arguments.
Post Reply