Procedures


Syntax
Procedure[.<type>] name(<variable1>[,<variable2>,...]) 
  ...
  [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.

Note: For returning strings from DLLs, see DLLs

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$

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