Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Function Syntax

edited May 2012 in Questions Posts: 22

More simpleton questions, sorry in advance, but thanks in advance for your patience.

I am having an issue wrapping my head around functions. How to call them? Do they work in a linear fashion, and you do not have to call them? How do you pass parameters, why sometimes the are formatted as such:

function EnemyHorde:init()

and other times:

function setTest(t)

and sometimes just:

function nextTest()

I am having a problem locating a function tutorial or syntax definition. I have searched in the forums, and also the reference. I have looked at LUA specific references and I am still at a loss.

If you can point me to a reference for a beginner that would be great, or at least a few simple examples or exercises.

Thanks again,
Munkie

Comments

  • Posts: 179

    A function is defined like:

    function PrintStuff(stuff)
        print(stuff)
    end
    

    You would then call it like:

    PrintStuff("Hello World")
    

    If the function is part of a class called PrintRobot, you would define the function like this:

    function PrintRobot:PrintStuff(stuff)
    

    You can also just make a namespace (basically a table) to hold all your functions and do it the same way, ie...

    MyNameSpace = {}
    function MyNameSpace:MyFunction()
    

    Hope that helps.

  • edited May 2012 Posts: 22

    Thanks for the quick response @vega. It kinda clears some things up but raises other questions. What is "stuff". Why use a function? Does the function get defined in the setup? Can you have nested functions?

    Edit - Ok, I see that "stuff" is a parameter that is passed in to the function to be printed. Do parameters ever pass out of a function to be used in other functions or elsewhere in the code?

    Thanks again,
    Munkie

  • Posts: 447

    a function can return a value if that's what you mean.

    Examples

    function double(n)
      return 2*n
    end
    
    function quadruple(n)
      return double(double(n))
    end
    
    function fib(n)
      if n==0 or n==1 then return 1 end
      return fib(n-1)+fib(n-2)
    end
    
  • edited May 2012 Posts: 35

    Some of it pertains to basic programming concepts (functions, arguments, returning or altering values), but how all of that is put together here is language-specific.

    A function doesn't have to receive or return any arguments. Whether it receives any arguments, it can access anything in global scope. Without declaring a variable 'local' explicitly, things usually are in global scope.

    It can take a number of arguments, and "PrintStuff" takes one (we don't know what, and PrintStuff doesn't really care) we'll call 'stuff'. PrintStuff doesn't know what it is either.

    When it's called with "Hello World", 'stuff' becomes "Hello World", then 'PrintStuff' passes it on to 'print' (still as 'stuff' - 'print' doesn't know what it is yet either, it's just something named 'stuff' - 'print' won't necessarily know it by that name though, that depends on what argument[s] 'print' has named in its function definition...and so on down the line.)

    Functions are a convenient way of passing messages back and forth, if you will, between bits of code. I'm sure much of this is not technically correct -- just trying to help with a general concept of 'functions' as programming tools, not worry too much about scope or instantiation of variables (or functions for that matter.)

    They're 'sub-routines' if you prefer to think of them that way, a way to organize your code. The one thing to keep in mind is that, whatever you do to the named 'stuff' within the function stays within the function. (You can give tables as arguments to functions, however, and alter the contents/elements of the tables.) If I pass a function a hard-coded "10", it can't change what I've passed it to "11" of course -- but it can add 1 to the variable 'stuff' that is assigned the value 10 when the function is invoked.

    If you wanted to change the variable(s) you pass in to a function, you can return the desired result(s) with the 'return' statement and assign the value of the function (what it returns) to the variable(s) you want changed:

    local stuff=10
    AddStuff(stuff)
    
    print(stuff)
    -- stuff is still 10
    
    stuff=AddStuff(stuff)
    print(stuff)
    -- stuff should be 11 now
    
    function AddStuff(stuff)
     stuff=stuff+1
     return stuff
    end
    

    I hope that helps some.

Sign In or Register to comment.