First Steps with Lua - FunctionsDeutsch


You are here:LuaFunctions



Functions


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

Test()
The Result:
Hallo Welt

Rail-Diagramm funcbody

Return values are defined with return.

Test-Programm function_3.lua
function test()
	return  "Hallo", "Welt"
end

v1, v2 = test()
print( v1 )
print( v2 )
The Result:
Hallo
Welt
Multiple return values are possible.

Test-Programm function_4.lua
function test()
	return  "Hallo", "Welt"
end

v = test()
print( v )
The Result:
Hallo
If you return multiple values without enough receivers, the additinal return values are ignored.

Test-Programm function_2.lua
function test()
	return  "Hallo Welt"
end

print ( test() )
The Result:
Hallo Welt

Functions with Parameter

Test-Programm function_par_1.lua
function summe( _v1, _v2)
	return  ( _v1 + _v2 )
end

print( summe( 1, 2 ) )
print( summe( 2, 3 ) )
The Result:
3
5

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

print( summe( 1, 2 ) )
print( a )
print( _v1 )
The Result:
3
3
nil

Test-Programm 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 )
The Result:
3
vorher
nil