Create your own Function library - Part #1

Ever want a set of repeating functions for your code? YES, of course, there would be no one that would not want this. There are quite a few libraries that are really worth using, like from CrawlSpaceLibrary, JonBeebe, Ricardo to name a few prominently used ones. Here is a mini series that teaches you , how to create your own Library

First things first, what is a library.
A library is a collection of functions that can be used modularly by including this file in your project.

How do I use a library
A library is used by referencing it in the project for it to be made available. (NOTE: However there is one other LUA variant (Gideros) that does not require the inclusion as it will run all the lua files, so the code automatically gets included.)

So the code to include the lua library is as follows
local myLib = require("library")

where library.lua is the file that contains the functions we want to use.

Is that the only way?
No, not really, there is another way and that is
require("library")

The major difference between the two is that in the first example, we have a handle to the library (in case we return some object to work with) that has associated functions, where as the second declaration above just adds the functions to the project and can be referred to directly.

So for example, when we use the physics library, we use
local physics = require("physics")
 physics.start()
 --
 --
 physics.stop()
 --
 --
 require("library") -- contains function1 and function2
 function1()
 function2()

Hope you get the idea here of the difference between the two methods of declaration.

So, How do I create a library?
For a library, the first line has to allow it to specify that this is a module and needs to be visible from everywhere (depending on the scope of the functions and variables)

module(...,package.seeall)

So let us create a function library that will be included by just using require("library")

function function_add(a, b)
    return a + b
  end

  function function_sub(a, b)
    return a - b
  end

now to use this library, we use
require "library")
 
 print(function_add(4,7))
 print(function_sub(9,3))

Hope you get how libraries work with the example above.

NOTE: Look there is *NO* local in front of the declaration, as the functions declarations have no be *non* local for them to work or be called from outside of the library.

That concludes our first mini-series, in the next one we shall look at the second way to declare and work with libraries.

You comments are appreciated, so are your million dollar checks, please do not be anonymous on your checks, please sign them ;)

cheers,

?:)

Comments

  1. Wow.. Thank you this may seem strange but i was waiting for someone to explain this to me. For some reason i couldnt figure it out on my own and this set me straight!

    Thanks again

    ReplyDelete
  2. Thank you O wise Guru.
    Live long and prosper!

    ReplyDelete

Post a Comment

Popular Posts