Procedures


Syntax
Procedure[.<type>] name(<parameter1[.<type>]> [, <parameter2[.<type>] [= 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).

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.

Arrays can be passed as parameters using the Array keyword, lists using the List keyword and maps using the Map keyword.

A procedure can return a value or a 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 exits immediately the procedure, even when its called inside a loop.
ProcedureReturn can't be used to return an array, list or a map. For this purpose pass the array, the list or the map as parameter to the procedure.

If no value is specified for ProcedureReturn, the returned value will be undefined (see inline assembly for more information).

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 convention.

Selected procedures can be executed asynchronously to the main program by using of threads.

Example: Procedure with a numeric variable as return-value

  Procedure Maximum(nb1, nb2)
    If nb1 > nb2
      Result = nb1
    Else
      Result = nb2
    EndIf
  
    ProcedureReturn Result
  EndProcedure 
  
  Result = 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: List as parameter

  NewList Test.Point()

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

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

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

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

  DebugList(10, Test())

Example: Array as parameter

  Dim Table.Point(10, 15)

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

  Procedure TestIt(c.l, Array 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(<parameter1[.<type>]> [, <parameter2[.<type>] [= DefaultValue]>, ...])
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 and optional parameters, if any).

For advanced programmers DeclareC is available and will declare the procedure using 'CDecl' instead of 'StandardCall' calling convention.

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