Mit Funktionen können wir unseren Code wesentlich besser strukturieren, indem wir Teile des Programms in eben solche auslagern. Durch sie wird der Quellcode übersichtlicher und die entsprechenden Teile wiederverwertbar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
-- Tutorial 6 function sagHallo() return "hallo" end text = sagHallo() function sage(text) print(text) end sage("hallo nochmal") function sage(text, mal) mal = mal or 1 var1 = 10 for i = 1, mal do print(text) end end print(var1) sage("sag mal...") function getPosition() local x = 10 local y = 20 return x, y end posx, posy = getPosition() print("x:"..posx.." y:"..posy) |