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. To access main code variables, they have to be shared them by using Shared or Global keywords. A procedure can return a result if necessary. You have to set the type after 'Procedure' and use the ProcedureReturn keyword at any moment inside the Procedure.

Example :

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)
PrintNumberN(Result)
End 

 

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

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