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
- 10 RISC OS gift ideas for Christmas (News:)
- Drag'N'Drop Autumn edition now available (News:)
- !DualHead puts 2 screens in one (News:)
- RISC OS London Show 2017 - Notes from the talks (News:6)
- November News (News:)
- !Organizer 2.28 reviewed (News:2)
- !OBrowse reviewed (News:10)
- Aemulor (Gen:16)
- DDE reaches release 28 and above (News:)
- Elesar quicks dispels stormy clouds (News:2)
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: 484
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