lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Maybe its helps when understanding C function pointers better, its no
magic difference. In Lua its just,that every function is actually a
function pointer. Maybe this example helps as well:

function a(arg)
  print("a prints: "..arg)
end

function b(arg)
  print("b prints: "..arg)
end

x = a
y = b

function do()
  x("one")
  y("two")
  print()
  x, y = y, x
end

do()
do()
do()

Now guess what it will print (as long I didnt make a typo)

On Fri, Feb 4, 2011 at 6:26 AM, Steve Litt <slitt@troubleshooters.com> wrote:
> On Friday 04 February 2011 00:04:46 Geoff Leyland wrote:
>> On 4/02/2011, at 5:56 PM, Steve Litt wrote:
>> > Thanks Geoff,
>> >
>> > I think it does help. So I define my new function print() as taking
>> > argument str, so obviously when the program is run str is "Hello World".
>> > Then I pass that str to oldprint(), which is a reference to print(),
>> > which has already been defined as taking an argument, so no further
>> > definition or declaration needed.
>> >
>> > Does that about sum it up?
>>
>> Yep.  "oldprint(), which is a reference to *the original* print()"
>> If none of these functions were called "print" or anything like it, and
>>  there weren't languages where print was part of the language and not just
>>  a function, I think it'd all be much simpler to understand.
>
> OK, I think I have it. Thanks.
>
> SteveT
>
>
> Steve Litt
> Recession Relief Package
> http://www.recession-relief.US
> Twitter: http://www.twitter.com/stevelitt
>
>
>