lv |
||
[ | uv | ] |
fv |
int &weird(int &x) { return ++x; } int main() { int x = 1; int &y = weird(x); printf("x = %d, y = &d \n", x, y); y++; printf("x = %d, y - %d \n", x, y); }Ouput:
x = 2, y = 2 x = 3, y = 3Bonus question: If we change the ++x to a x++, then the code will no longer compile. Why not?
MyTable = {[1] = 2, [2] = 2, [5] = 1, [6] = 1} s1 = 0 s2 = 0 for k,v in pairs(MyTable) do s1 = s1 + k s2 = s2 + v end print(s1,s2)Output:
15 6
x = 2 y = 3 function twist() local x = 7 x,y = y, x print(x,y) end twist() print(x,y)Output
3 7 2 7
ft = {} ft[1] = function(x, y) return y,x end ft[2] = function(x, y, z) return z, y, x end a, b, c = ft[1](1,2,3) d, e, f = ft[2](4,5,6) print(a, b, c) print(d, e, f)Output:
2 1 nil 6 5 4