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 WeltMultiple return values are possible.
Test-Programm function_4.lua
function test() return "Hallo", "Welt" end v = test() print( v )
The Result:
HalloIf 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
- Parameter start with _ (it's a convention, not a must)
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
- Parameter are local.
- Other variable are defined global.
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
- If new varibales are defined with local, then they are only defined inside the block.