log in | register | forums
Show:
Go:
Forums
Username:

Password:

User accounts
Register new account
Forgot password
Forum stats
List of members
Search the forums

Advanced search
Recent discussions
- I want to digitise some A3010 software - ONLY COPY (Gen:5)
- RISC OS ports website (News:5)
- For Swaps/Trade Acorn User/Archi World Magazine Collection (Gen:5)
- More Acorn Magazine nostalgia (News:)
- RISC OS London Show 2018 (News:)
- NetSurf or Iconbar? (Site:3)
- September News round-up (News:)
- DDE tools updated to DDE28c (News:)
- Orpheus hits crowdfunding target (News:2)
- Orpheus launch crowdfunding campaign (News:5)
Latest postings RSS Feeds
RSS 2.0 | 1.0 | 0.9
Atom 0.3
Misc RDF | CDF
Site Search
 
Article archives
The Icon Bar: Programming: RETURN non-existent variable
 
  RETURN non-existent variable
  swirlythingy (20:03 15/2/2013)
  nunfetishist (11:33 16/2/2013)
    swirlythingy (21:49 16/2/2013)
 
Martin Bazley Message #121923, posted by swirlythingy at 20:03, 15/2/2013

Posts: 460
In BASIC, I frequently use constructs such as the following:

a%=FNa(b%,c%)
(...)
DEF FNa(RETURN b%,RETURN c%)
b%=2:c%=3
=1


This initialises the variables b% and c%, regardless of whether they actually existed prior to FNa being called.

I'm not sure how far this tolerance in passing a reference to something which doesn't exist yet extends, though. What happens if FNa tries to read b% and c% prior to writing to them? Limited experimentation suggests that RETURNed parameters are automatically initialised to 0 if they didn't exist when the procedure/function was entered, but is this defined behaviour and/or should I rely on it?

The BBC BASIC reference manual doesn't appear to cover such subtleties.
  ^[ Log in to reply ]
 
Rob Kendrick Message #121924, posted by nunfetishist at 11:33, 16/2/2013, in reply to message #121923
nunfetishist
Exposing morons since 1981

Posts: 485
The BBC BASIC reference manual doesn't appear to cover such subtleties.
This is why you should be using a more modern and more carefully described language smile

On answering your question, I always assumed that such variables are initialised as zero and it was safe to assume that: but if it isn't, it's the caller's fault, and not the function's, IME (in case you're writing a library).

Also, I think if this was not the case, the runtime variable creation hack using EVAL and RETURN variables wouldn't work.
  ^[ Log in to reply ]
 
Martin Bazley Message #121928, posted by swirlythingy at 21:49, 16/2/2013, in reply to message #121924

Posts: 460
On answering your question, I always assumed that such variables are initialised as zero and it was safe to assume that: but if it isn't, it's the caller's fault, and not the function's, IME (in case you're writing a library).
In this case I'm writing both the caller and the function. The problem stems from the wide variety of circumstances in which I am attempting to use the same procedure. Depending on a switch, one of its parameters is either an input (and the program should error if it is not initialised) or an output (the program is able to derive the value of that parameter for itself and pass it back to the caller). Determining whether or not it has been "initialised", however, is more tricky - and in some other circumstances, it is perfectly valid for the same parameter to be write only!
Also, I think if this was not the case, the runtime variable creation hack using EVAL and RETURN variables wouldn't work.
No, that's safe, because you never read the value of the variable in the EVAL statement, you only assign it.
  ^[ Log in to reply ]
 

The Icon Bar: Programming: RETURN non-existent variable