Mikroelektronika MIKROE-724 データシート

ページ / 726
202
mikoBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
Example: 
This example shows how to declare a function which returns a complex type.
program Example
structure TCircle       ‘ Structure
  dim CenterX, CenterY as word
  dim Radius as byte
end structure
dim MyCircle as TCircle ‘ Global variable
sub  function  DefineCircle(dim  x,  y  as  word,  dim  r  as  byte)  as  TCircle  ‘  DefineCircle 
function returns a Structure
  result.CenterX = x
  result.CenterY = y
  result.Radius = r
end sub
main:
  MyCircle = DefineCircle(100, 200, 30)         ‘ Get a Structure via function call
  MyCircle.CenterX = DefineCircle(100, 200, 30).CenterX + 20  ‘ Access a Structure field 
via function call
  ‘                  |------------------------| |-----|
  ‘                     |                         |
  ‘                  Function returns TCircle     Access to one field of TCircle
end.
Forward declaration
A  function  can  be  declared  without  having  it  followed  by  it’s  implementation,  by  having  it  followed  by  the  forward 
procedure. The effective implementation of that function must follow later in the module. The function can be used after 
a forward declaration as if it had been implemented already. The following is an example of a forward declaration:
program Volume
dim Volume as word
sub function First(dim a as worddim b as word) as word forward
sub function Second(dim c as word) as word
dim tmp as word
  tmp = First(2, 3)
  result = tmp * c
end sub
sub function First(dim a, b as word) as word
  result = a * b
end sub
main:
  Volume = Second(4)
end.