Procedures


Syntax
Procedure[.<type>] name(<variable1>[,<variable2 [= DefaultValue]>,...]) 
  ...
  [ProcedureReturn value]
EndProcedure 
Description
A Procedure is a part of code independent from the main code which can have any parameters and it's own variables. In PureBasic, a recurrence is fully supported for the procedures and any procedure can call it itself. At each call of the procedure the variables inside will start with a value of 0 (null). To access main code variables, they have to be shared them by using Shared or Global keywords (see also the Protected and Static keywords).

A procedure can return a result (value, string) if necessary. You have to set the type after Procedure and use the ProcedureReturn keyword at any moment inside the procedure. A call of ProcedureReturn exists immediately the procedure, even when its called inside a loop.

The last parameters can have a default value (need to be a constant expression), so if these parameters are omitted when the procedure is called, the default value will be used.

Like variables also arrays and linked lists can be passed as parameters to the procedure.

Note: To return strings from DLLs, see DLLs. For advanced programmers ProcedureC is available and will declare the procedure using 'CDecl' instead of 'StandardCall' calling convension.

Example: Procedure with a long variable as return value

  Procedure.l Maximum(nb1.l, nb2.l)
    If nb1>nb2
      Result.l = nb1
    Else
      Result = nb2
    EndIf
  
    ProcedureReturn Result
  EndProcedure 
  
  Result.l = Maximum(15,30)
  Debug Result

Example: Procedure with a string as return value

  Procedure.s Attach(String1$, String2$)
    ProcedureReturn String1$+" "+String2$
  EndProcedure 

  Result$ = Attach("PureBasic", "Coder")
  Debug Result$

Example: Parameter with default value

  Procedure a(a, b, c=2)
    Debug c
  EndProcedure

  a(10, 12)      ; 2 will be used as default value for 3rd parameter
  a(10, 12, 15) 

Example: Linked list and Array as parameters

  ; Linked list and Array as parameters
  ;
  ; Linked list
  ;
  ;

  NewList Test.Point()

  AddElement(Test())
  Test()\x = 1
  AddElement(Test())
  Test()\x = 2

  Procedure DebugList(c.l, ParameterList.Point())

    AddElement(ParameterList())
    ParameterList()\x = 3

    ForEach ParameterList()
      MessageRequester("List", Str(ParameterList()\x))
    Next
 
  EndProcedure

  DebugList(10, Test())

  ;
  ; Table (array)
  ;
  ;

  Dim Table.Point(10, 15)

  Table(0,0)\x = 1
  Table(1,0)\x = 2

  Procedure TestIt(c.l, ParameterTable.Point(2))  ; The table support 2 dimensions

    ParameterTable(1, 2)\x = 3
    ParameterTable(2, 2)\x = 4
 
  EndProcedure

  TestIt(10, Table())

  MessageRequester("Table", Str(Table(1, 2)\x))

Syntax
Declare[.<type>] name(<variable1>[,<variable2>,...])
Description
Sometimes a procedure need to call another procedure which isn't declared before its definition. This is annoying because the compiler will complain 'Procedure <name> not found'. Declare can help in this particular case by declaring only the header of the procedure. Nevertheless, the Declare and real Procedure declaration must be identical (including the correct type).

Example:

  Declare Maximum(Value1, Value2)
  
  Procedure Operate(Value)
    Maximum(10, 2)      ; At this time, Maximum() is unknown.
  EndProcedure
  
  Procedure Maximum(Value1, Value2)
    ProcedureReturn 0
  EndProcedure