Procedure[.<type>] name(<parameter1[.<type>]> [, <parameter2[.<type>] [= DefaultValue]>, ...])
  [ProcedureReturn value]
A Procedure is a part of code independent from the main code which can have any parameters and its 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 by using Shared or Global keywords (see also the Protected and Static keywords).

The last parameters can have a default value (needs 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
      Result = nb2

    ProcedureReturn Result

  Result = Maximum(15, 30)
  Debug Result

Example: Procedure with a string as return-value

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

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

Example: Parameter with default value

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

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

Example: List as parameter

  NewList Test.Point()

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

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

    ParameterList()\x = 3

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


  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


  TestIt(10, Table())

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

Example: Static, dynamic array and passing a Structure to a Procedure

  Structure Whatever
    b.l[2]          ; Static array (Standard C) with 2 values b[0] and b[1], not resizable
    Array c.l(3,3)  ; Dynamic array with 16 values c(0,0) to c(3,3), resizable with ReDim()


  Procedure MyProcedure(*blahblah.Whatever)
    *blahblah\a = 5
    *blahblah\b[0] = 1
    *blahblah\b[1] = 2
    *blahblah\c(3,3) = 33

  Debug MyVar\a
  Debug MyVar\b[0]
  Debug MyVar\b[1]
  Debug MyVar\c(3,3)

Example: Call a function by its name

   Prototype Function()

  Runtime Procedure Function1()
      Debug "I call Function1 by its name"

  Procedure LaunchProcedure(Name.s)
      Protected ProcedureName.Function = GetRuntimeInteger(Name + "()")

  LaunchProcedure("Function1") ; Display "I call Function1 by its name"

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


  Declare Maximum(Value1, Value2)

  Procedure Operate(Value)
    Maximum(10, 2)      ; At this time, Maximum() is unknown.

  Procedure Maximum(Value1, Value2)
    ProcedureReturn 0