Charles Pegge
12-07-2009, 18:17
Functions within Functions
I've always wanted to do this in Basic: create small transient functions inside a function or sub but isolated from the rest of the program. This sort of function is often known as a lambda (as used in Functional languages and Ruby)
Inner Functions are only visible from inside the function, sub or method that contains them.
They can access static variables with the parent functions but their locals are private.
The subs and functions can be expressed in various formats, as you see in the example below. The main body is not terminated with end ... but is surrounded by brackets instead. Alternatively, if the main body only contains a single expression then it begins with an equals sign and is not contain within brackets.
I hope this makes sense and fits in well with general Basic syntax.
I've set the nesting level to an arbitrary maximum of 7 as anything more than 3 levels would be :x !!
Latest Oxygen http://community.thinbasic.com/index.php?topic=2517
'----------------------------------------
'INNER FUNCTIONS
'========================================
uses "oxygen","file"
dim src as string
src = "
#basic
'---------------------
function fun(p) as long
'=====================
local a=p
local c=20000
static b=4
;----------------------
'INNER FUNCTIONs --->>>
function f(v) as long = v*20
print str f a
function f(v) as long (function=10+v*10)
print str f a
function f(byval v as long) as long (local c=13 : function=v*3+b+c )
print str f a
function f(v) as long
(
local c=17
function=c+v*3
)
print str f a
function=0
end function
fun 1
"
'file_save ( "t.txt", o2_prep src )
'msgbox 0, o2_prep src
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
I've always wanted to do this in Basic: create small transient functions inside a function or sub but isolated from the rest of the program. This sort of function is often known as a lambda (as used in Functional languages and Ruby)
Inner Functions are only visible from inside the function, sub or method that contains them.
They can access static variables with the parent functions but their locals are private.
The subs and functions can be expressed in various formats, as you see in the example below. The main body is not terminated with end ... but is surrounded by brackets instead. Alternatively, if the main body only contains a single expression then it begins with an equals sign and is not contain within brackets.
I hope this makes sense and fits in well with general Basic syntax.
I've set the nesting level to an arbitrary maximum of 7 as anything more than 3 levels would be :x !!
Latest Oxygen http://community.thinbasic.com/index.php?topic=2517
'----------------------------------------
'INNER FUNCTIONS
'========================================
uses "oxygen","file"
dim src as string
src = "
#basic
'---------------------
function fun(p) as long
'=====================
local a=p
local c=20000
static b=4
;----------------------
'INNER FUNCTIONs --->>>
function f(v) as long = v*20
print str f a
function f(v) as long (function=10+v*10)
print str f a
function f(byval v as long) as long (local c=13 : function=v*3+b+c )
print str f a
function f(v) as long
(
local c=17
function=c+v*3
)
print str f a
function=0
end function
fun 1
"
'file_save ( "t.txt", o2_prep src )
'msgbox 0, o2_prep src
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec