Procedure RETURN of an Array

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
JPinVA
Posts: 2
Joined: Wed 26 Feb 2020, 16:12

Procedure RETURN of an Array

Post by JPinVA »

Arrays are passed to procedures by reference. As such, the RETURN term (used to force a variable pass by reference instead of by value) is not needed for arrays.

My question is does BB4W care if I include the RETURN term for an array? I'm only asking for situations where the calling routine or main program has already defined the array being passed, not the situation where the routine itself is defining the array (where a RETURN is required).

In other words, is DEF PROC_TestArray (A%()) the same as DEF PROC_TextArray (RETURN A%())?

If you're wondering why I'm asking, I prefer the latter because I have many routines that pass several arrays, but only one is being changed in the routine. The RETURN is a quick visual reminder of which one is being changed in the routine.

I have several routines that use the redundant RETURN, and they work fine. I just need some assurance that I'm not doing something that will come back and bite me later.
Ivan
Posts: 127
Joined: Tue 07 May 2019, 16:47

Re: Procedure RETURN of an Array

Post by Ivan »

Hi JPinVA

If I understand correctly, you are passing an array to a procedure (by value):

DEF PROC_TestArray (A%())

And in:

DEF PROC_TextArray (RETURN A%())

You are returning an array from a procedure (by reference).

For my part it's two different operations.

Regards,

Ivan
BBC Model B - 1984-1989. 6502 assembler, Unicomal 1988-1994, Some C and C++, Pascal 1990-1994. Bought a copy of BBC-BASIC 2007, but started to program at a daily basis 2019. C++ in 2021.
DDRM

Re: Procedure RETURN of an Array

Post by DDRM »

Hi Ivan and JPinVA,

Arrays are always passed by reference: JPinVA's question is whether it's OK to use "RETURN" anyway, even though it's redundant. I didn't answer immediately because, though I should think the answer is "yes, it's OK", I don't know for sure. For a definitive answer, I suggest asking Richard, either on the groups.io site, or by private email.

One reason I think it will be OK (apart from the fact that you've tried it and it works!) is a (possible over-)interpretation of the manual section on getting a (new) array back from a procedure using return: in it, Richard says "If you pass an array which already exists, it must have an identical format to the declaration within the function or procedure." - which implies it's OK to pass in an existing array as a RETURN parameter (even, presumably, if you then don't declare it in the procedure). Either way all you are passing as a parameter is a pointer to an array: the only difference is when you point that pointer at the actual data structure.

Best wishes,

D