RETURN from FN or PROC

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

RETURN from FN or PROC

Post by MattC »

Hi,

Which is the more appropriate to use?:

Code: Select all

A% = FNsubroutine(A%)
...
DEF FNsubroutine(a%)
= 10
or

Code: Select all

PROCsubroutine(A%)
...
DEF PROCsubroutine(RETURN a%)
a% = 10
ENDPROC
Both return the value to the original variable. Is there a usual or more correct one to use for this? I can see in some cases it would be more obvious depending on the circumstances, but not always.

Matt
DDRM

Re: RETURN from FN or PROC

Post by DDRM »

Hi Matt,

As you say, it depends on circumstances. I'd tend to use the FN approach if I only wanted one result, and the PROC approach if I wanted to return more than one. Using FN also means you can use it like a variable directly in an expression:

product = FNGetResultOne(inputs) * FNGetResultTwo(inputs2)

You'll see lots of examples of this in the manual.

Using a function means the result needs to be returned to a variable, which can (but doesn't have to be) a different one, while using RETURN and a procedure will always return it in the same variable (overwriting the old value) - either situation may be desirable, or not!

Hope that helps,

D
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: RETURN from FN or PROC

Post by MattC »

Thanks. It's good to get other opinions.

Matt