Procedure
A Procedure is a subroutine with local memory and optional parameters. You may pass parameters to the subroutine when calling it. You may also use global Variables of the main program/superior Class.
See also: What is a subroutine?
Local Variables
Lokal variables can be declared in the Method, that are valid only until you exit the Method. Passing parameters requires additional memory until the subroutine is exited. Without Parameters and/or local variables its just the return address (2 or 3 Byte). See also: Dim, Chapter visibility of variables and keyword „static“
Typecasting
If the parameter passed is of a different type than in the subroutine, the numeric data type will be typecast automatically. Example, if you pass a „Word“ variable and the Subroutine expects a Byte, then the Word will be converted to a Byte.
Parameter Passing
Parameters can be passed byVal Copy or byRef Reference1) to the Subroutine. The parameters are exchanged via the program stack (see avr.Stack).
Paramater passing per Copy (byVal):
- Object properties and -Methods with return value
- Methods with return value
Paramater passing per Reference (byRef):
- Memory-Object "sram" (Variables, Arrays, Structures, ..)
- Variables incl. Structures
The keyword „byVal“ is the default method and can be omitted.
InitialValue Parameter
InitialValue is a feature with which you can optionally assign an initial value parameters:
procedure test(a as byte, b as word = 456) endproc procedure test1(a as byte = 123, b as word) endproc
The call can then be different by then the optional parameters, then the omitted parameter when called by the initial value will be replaced automatically:
test(123,333) test(100) 'the optional second parameter is given the initial value test1(,444) 'the first parameter is replaced by the initial value
Method-Overload
Methoden-Overload is a feature in which multiple methods with the same name can be defined with different parameter types or return values. The call is then determined by the number and type of parameters.
test(123) 'methode #1 is called test(123,456) 'methode #2 is called a = test(123) 'methode #3 is called 'methode #1 procedure test(a as word) endproc 'methode #2 procedure test(a as byte, b as word) endproc 'methode #3 function test(a as byte) endfunc
Assignment
Implemented as of Version | 2015.r1 |
---|
With the keyword assigns at the last parameter of a method specifies a method (Procedure), a value can be assigned in the source code.
test(100) = 200 '[...] procedure test(a as byte, assigns b as word) print "parameter = ";str(a) print "assignment = ";str(b) endproc
Inline
The optional keyword inline makes a inline-method.
Namenspace
Methods can access the global variables of the superior Class. A variable „a“, declared in the main program, is also accessible by a Procedure/Function. However, if you declare a variable of the same name in a Procedure/Function or define it as a parameter, then this variable will be the only one accessible.
Recursive/Nested Calls
Luna Methods Reentrancy are reentrant and can be nested or called recursively, or called by one or more Interrupts of the main program.
Syntax
Syntax:
-
- Program Code
- [ Return ]
- Program Code
- endProc
- Name: Identifier of the Subroutine
- Var: Identifier of the Parameter Variable
Example 1:
' Main Program dim value as integer dim s as string output("My Number: ",33) ' Call the Subroutine 'also possible value = 12345 s = "Other Number: " call output(s,value) do loop ' Subroutine procedure output(text as string, a as byte) dim x as integer ' locally valid Variable x=a*100 print text+str(x) endproc
Example 2:
struct point byte x byte y endstruct dim p as point p.x=23 test(p) print "p.x = ";str(p.x) ' output: p.x = 42 do loop procedure test(byRef c as point) print "c.x = ";str(c.x) ' output: c.x = 23 c.x = 42 endproc
Example 3:
test(text) ' output: c.PString(0) = "hello" test("ballo") ' output: c.PString(0) = "ballo" do loop procedure test(byRef c as data) print "c.PString(0) = ";34;c.PString(0);34 endproc data text .db 5,"hello" enddata