Home

Static Items In Functions With Falcon @ 2008-10-02 22:21:25
Filed under: Code  Tech 
I've seen statics in classes but I've never used them in functions before. They are nifty!

#!/usr/bin/env falcon
// Simple example of static function usage in falcon

directive version=0x000001


function hello_you( )
    // static means the function remembers these items call after call
    static
        old_name = nil
    end

    // Only say someone was here is old_name isn't nil
    if old_name != nil
        printl( old_name, " was just here!")
    end
    // Ask for their name and say hello
    print( "What is your name? " )
    name = input()
    printl( "Hello ", name)
    old_name = name
end


printl("(Ctrl+c to end this madness ...)")
while true
    hello_you( )
end


Or a similar type thing with post function workflow ....

#!/usr/bin/env falcon
// Simple example of post function calling via registered functions

directive version=0x000001


// mock simple functions
function one( )
    printl( "ONE!" )
end


function two( )
    printl( "TWO!" )
end


function three( )
    printl( "THREE!" )
end
// --------------------


// Map what a user will call to execute a function
register = ["1" => one, "2" => two, "3" => three]


// Caller, executer and holder of other function workflow
function fun( nextfuncno )
    static
        msg = "Executing 1"
        func = one
    end

    printl( msg )
    func()
    try
        msg = @ "Executing $nextfuncno"
        func = register[nextfuncno]
    catch
        printl( @ "No registered function named $nextfuncno\nExiting..." )
        exit(1)
    end
end


printl("(Ctrl+c to end this madness ...)")
while true
    print( "What function should I call after the existing function? " )
    fun( input() )
end

 digg it   seed it   del.icio.us   ma.gnolia
Tags:    


 
Ohloh profile for ashcrow View Steve Milner's profile on LinkedIn A Django joint.
© 2007-2008 Steve 'Ashcrow' Milner | Studio7designs | Arbutus Photography