Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- closures are anonymous function in a function
- local function currentNumber() -- scopes can be used in functions aka local or global
- i = 0 -- create a variable called 'i' and assigned it to a number '0'
- print("lol")
- return function() -- create an anonymous function and return it (also called a closure)
- i = i + 1 -- add 1 to the variable i
- print("lol")
- return i -- return the variable i so the print() function will print the value inside the 'i' variable
- end
- end
- print("==================================")
- -- you can assigned a function to a variable
- local first = currentNumber() --if a function was assigned to a variable then the function will execute
- print("==================================")
- print(first()) -- prints out the return values in the function
- print(first) -- PROBLEM HERE: why does it print out the function id or something, it didn't print out the current value of the variable
- -- 'i'...
- print("==================================")
- print(currentNumber()) -- prints out what needs to be printed in function and prints out the function id
- print("==================================")
- currentNumber() -- execute the function block
- print("======================================")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement