PDA

View Full Version : Inner Functions



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

kryton9
12-07-2009, 21:18
This looks really nice Charles. I played with Ruby again last week to decide if I wanted to use Ruby on Rails on my website... coming from c++, Ruby syntax looks nice, but still when you get into it, Ruby syntax is not as nice as Basic. I would say Python after Basic has the nicest looking syntax, but that is another discussion. I guess what I am trying to say is-- the syntax and styling you are coming up with is very logical and appealing to me. I couldn't test out the code yet, I have been installing various versions of windows at night and then playing with friends online during the day on the weekend.

Petr Schreiber
12-07-2009, 21:58
Example worked fine here,

thanks Charles! The syntax is good I think, does it work like macro or is it really mini function?

Charles Pegge
12-07-2009, 23:51
Hi Kent, I have not tried Ruby but by reading the literature I am a concept pirate :)

Yes Petr, these are true functions - the only restriction is that you cannot legitimataly declare static variables within them and that they all share the static variables of the primary/parental function.

It is also possuble to use macros to perform similar operations. A macro can be used as a pseudo sub or it can be used as a pseudo function. But macros have their limitations and pseudo functions bypass operator precedence evaluation so you must pay close attention to the brackets.

Here is a pseudo function example:




'------------------------------
'INNER MACRO USED AS A FUNCTION
'==============================


uses "oxygen","file"
dim src as string

src = "
#basic

'---------------------
function fun() as long
'=====================

dim a=1,b

macro f(v) (98+(v*3)) end macro 'single line format

'multiline format (still producing single liner)

macro f(v)
(98+(v*3))
end macro


b = 1000+f(a)+10

function=b

end function

print `Result: ` str fun

"
'file_save ( "t.txt", o2_prep src )
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec