First-class functions/Use numbers analogously: Difference between revisions
First-class functions/Use numbers analogously (view source)
Revision as of 12:05, 4 March 2024
, 2 months agoAdded FreeBASIC
imported>Arakov |
(Added FreeBASIC) |
||
Line 565:
}
</syntaxhighlight>
=={{header|FreeBASIC}}==
FreeBASIC does not support first-class functions or function closures, which means that you cannot create a function that returns another function or that has a function defined inside it.
However, similar behavior can be achieved with subroutines and global variables.
<syntaxhighlight lang="vbnet">Dim As Double x = 2.0, xi = 0.5
Dim As Double y = 4.0, yi = 0.25
Dim As Double z = x + y, zi = 1.0 / (x + y)
Dim As Double values(2) = {x, y, z}
Dim As Double inverses(2) = {xi, yi, zi}
Dim Shared As Double m = 0.5
Function multiplier(a As Double, d As Double) As Double
Return a * d * m
End Function
For i As Byte = 0 To Ubound(values)
Dim As Double new_function = multiplier(values(i), inverses(i))
Print values(i); " *"; inverses(i); " *"; m; " ="; new_function
Next i
Sleep</syntaxhighlight>
{{out}}
<pre> 2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.1666666666666667 * 0.5 = 0.5</pre>
=={{header|Go}}==
|