Results 1 to 4 of 4

Thread: Inner Functions

  1. #1

    Inner Functions

    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 !!


    Latest Oxygen http://community.thinbasic.com/index.php?topic=2517


    [code=thinbasic]

    '----------------------------------------
    '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
    [/code]

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Inner Functions

    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.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Inner Functions

    Example worked fine here,

    thanks Charles! The syntax is good I think, does it work like macro or is it really mini function?
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4

    Re: Inner Functions

    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:

    [code=thinbasic]

    '------------------------------
    '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

    [/code]

Similar Threads

  1. C: string functions
    By danbaron in forum Science
    Replies: 2
    Last Post: 11-07-2011, 19:38
  2. B3D loading functions
    By Michael Hartlef in forum thinBasic vaporware
    Replies: 10
    Last Post: 10-02-2008, 21:18

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •