Wrox Programmer Forums
|
BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2
This is the forum to discuss the Wrox book Beginning Lua Programming by Kurt Jung, Aaron Brown; ISBN: 9780470069172
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 30th, 2007, 07:16 PM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default hazy on local function scope

On pg 110 there is a code script that has different functions calls calling the same function and keeps using the value that was left from the fuction call before.

> IncA(5)
> print(GetA())
5
> IncA(5)
> print(GetA())
10

Now I understand the concept that local fuctions inside functions use the variables of the outer fuction in their scope. But the rule states:

"Each time a function or local function statement is executed, or a function expression is evaluated, a new function is created."

So if a new local variable is being created for each new fuction call, why is it using the variable of the last fuction call? In my opinion this seems to be working a lot like setter methods in OOP, where the state of a fuction can be stored and changed throughout a program.
 
Old March 31st, 2007, 05:18 AM
Wrox Author
 
Join Date: Apr 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Brad,
Quote:
quote:So if a new local variable is being created for each new fuction call, why is it using the variable of the last fuction call?
One way to look at this is with an analogy to Lua tables. Whenever you see curly braces in Lua code, for example,
Code:
Tbl = {}
you know that a new table will be created when the expression is executed. Subsequent expressions involving Tbl, for example,
Code:
Tbl[3] = "hi"
won't create a new table. Similarly, the keyword 'function' is your clue that a new function will be created. When such an expression is evaluated, the function is "closed". That's what the following rule refers to.
Quote:
quote:"Each time a function or local function statement is executed, or a function expression is evaluated, a new function is created."
Calling a function is analagous to accessing an already-constructed table. Since calling a function doesn't involve the keyword 'function', it won't create a new function.
Here's an example:
Code:
local A = 10

local function Fnc(B)
  return A + B
end

print(Fnc(5))
First, a variable is created and bound to the name 'A', and then a function is created (or "closed") and bound to the name 'Fnc'. When Fnc is called, it is not created again. It does create a new variable for a copy of the argument (here, the value 5) and binds it to the name 'B', but it doesn't create a new variable for A. That way, the value of A persists between function calls.
Quote:
quote:In my opinion this seems to be working a lot like setter methods in OOP, where the state of a fuction can be stored and changed throughout a program.
Exactly.
--
Kurt
 
Old March 31st, 2007, 11:49 AM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Kurt! Im so used to C where if you want a persistent fuction variable, you have to use pointers. Its weird how difficult it is to see something so simple only because its slightly different.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Scope of Objects Handman BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 12 October 20th, 2008 10:06 AM
"Out of scope" var??? yankleber XSLT 2 March 31st, 2007 03:53 AM
what is the scope of PHP raaj Beginning PHP 1 February 13th, 2007 12:51 AM
scope problem jc Javascript 0 July 29th, 2003 09:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.