Erste Schritte in Lua - FunktionenEnglish


Sie sind hier: LuaFunktionen



Funktionen


Rail-Diagramm function
Beispielprogramm function_1.lua
function Test()
	print( "Hallo Welt" )
end

Test()
Das Ergebnis:
Hallo Welt

Rail-Diagramm funcbody

Beispielprogramm function_2.lua
function test()
	return  "Hallo Welt"
end

print ( test() )
Das Ergebnis:
Hallo Welt
Rückgabewerte werden mit return angegeben.

Beispielprogramm function_3.lua
function test()
	return  "Hallo", "Welt"
end

v1, v2 = test()
print( v1 )
print( v2 )
Das Ergebnis:
Hallo
Welt
Mehrfache Rückgabewerte sind möglich.

Beispielprogramm function_4.lua
function test()
	return  "Hallo", "Welt"
end

v = test()
print( v )
Das Ergebnis:
Hallo
Werden mehrere Werte zurückgegeben, aber nicht entgegengenommen, dann werden die zusätzlichen Werte 'geschluckt'.

Funktionen mit Parameter

Beispielprogramm function_par_1.lua
function summe( _v1, _v2)
	return  ( _v1 + _v2 )
end

print( summe( 1, 2 ) )
print( summe( 2, 3 ) )
Das Ergebnis:
3
5

Beispielprogramm function_par_2.lua
a = 'vorher'
function summe( _v1, _v2)
	a = _v1 + _v2
	return  a
end

print( summe( 1, 2 ) )
print( a )
print( _v1 )
Das Ergebnis:
3
3
nil

Beispielprogramm function_par_3.lua
a = 'vorher'
function summe( _v1, _v2)
	local a = _v1 + _v2
	return  a
end

print( summe( 1, 2 ) )
print( a )
print( _v1 )
Das Ergebnis:
3
vorher
nil