Topic variable: Difference between revisions

Added FreeBASIC
No edit summary
(Added FreeBASIC)
Line 53:
 
The word '''>R''' places the item on the return stack and the word '''R>''' retrieves it from the return stack - an experienced Forth programmer would optimize this definition even further. Note that for technical reasons all words listed cannot be used outside definitions, so it may be argued that Forth doesn't have topic variables.
 
=={{header|FreeBASIC}}==
The nearest thing FreeBASIC has to a topic variable is the name of the current function.
 
For example, if you declare a function called 'func' with a return type of Integer, the function behaves as though it contains a local variable called 'func' - defined at the top of its body - of type Integer. You can assign to this variable and, if you do, the return value of the function is the value this variable has when the function returns.
 
Alternatively, the keyword 'Function' can itself be used as an implicitly defined variable and behaves in exactly the same way as the function's name when used in this role. Similarly, the keywords 'Property' or 'Operator' can be used to return values from properties or operators respectively.
 
<lang freebasic>' FB 1.05.0 Win64
 
' Three different ways of returning a value from a function
 
Function Sum (x As Integer, y As Integer) As Integer
Sum = x + y '' using name of function
End Function
 
Function Sum2 (x As Integer, y As Integer) As Integer
Function = x + y '' using Function keyword
End Function
 
Function Sum3 (x As Integer, y As Integer) As Integer
Return x + y '' using Return keyword which always returns immediately
End Function
 
Print Sum (1, 2)
Print Sum2(2, 3)
Print Sum3(3, 4)
Sleep</lang>
 
{{out}}
<pre>
3
5
7
</pre>
 
=={{header|Go}}==
9,485

edits