RETURN from FN or PROC

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

RETURN from FN or PROC

Post by MattC » Fri 19 Apr 2019, 21:33

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
Administrator
Posts: 149
Joined: Mon 02 Apr 2018, 18:04

Re: RETURN from FN or PROC

Post by DDRM » Sat 20 Apr 2019, 09:10

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: 51
Joined: Mon 16 Apr 2018, 06:17

Re: RETURN from FN or PROC

Post by MattC » Sat 20 Apr 2019, 15:09

Thanks. It's good to get other opinions.

Matt

Post Reply