Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created December 4, 2021 06:17
Show Gist options
  • Save rozer007/4f925ad7b15d6c15a5fd09dfdee7890d to your computer and use it in GitHub Desktop.
Save rozer007/4f925ad7b15d6c15a5fd09dfdee7890d to your computer and use it in GitHub Desktop.
functions and closure in lua
Q1. Do we need to define the return type while defining a function ?
ans: No,There is no nedd to define a return type ofr a function in lua.
Q2.What is a variadic function in lua?
ans: variadic function are those function which accepts unknown number of arguments.
Q3.What is anonymous function in lua?
Ans: Anonymous function are those function which have body but doesn't have a name.
Q4.Does closure have access to all the locak variable in the program?
ans: No , closure will have only access to the local varible of the enclosing function.
Q1. function prt()
print("Hello pepcoder")
end
for i = 1,3,2 do
prt()
end
How many times " hello pepcoder" will be printed:
a)1
b)2
c)3
d) none
ans: b)
Q2.What will be the output of the code :
function prt(...)
for i,v in ipairs{...} do
io,write(i*v," ")
end
end
prt(1,2,3,4,5,6)
a) 1 4 9 16 25 36
b) 0 2 6 12 20 30
c) 0 1 2 3 4 5 6
d) 1 2 3 4 5 6 7
ans: a)
Q3. What will be the output of this statements :
function prt(...)
local sum=0
return function(...)
for i,v in ipairs{...} do
sum=sum + v
end
return sum
end
end
f=prt(1,2,3,4,5,6)
print(f(1,2,3,4,5))
a) 21
b) 15
c) will return function
d) none of the above
ans: b)
Q4. function counter()
local c=10
return function()
c=c-1
return c
end
end
c1=counter()
print(c1())
print(c1()) --> line 1
What will be output of line 1
a) 10
b) 11
c) 8
d) 9
ans: c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment