Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 30th, 2007, 07:16 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
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.
  #2 (permalink)  
Old March 31st, 2007, 05:18 AM
Wrox Author
 
Join Date: Apr 2006
Location: , , .
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
  #3 (permalink)  
Old March 31st, 2007, 11:49 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
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.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:23 PM.


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