SmartPascal
Sum
Function
Return the sum of an array of floating point values Math unit
 function Sum ( const Numbers : array of Double ) : Double;
Description
The Sum function returns the sum of the values in the supplied Numbers array of double values.
Related commands
Dec Decrement an ordinal variable
Inc Increment an ordinal variable
Sqr Gives the square of a number
Sqrt Gives the square root of a number
 
Example code : Examples of summing arrays and hard coded values
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  Math,   // Unit containing the Sum command
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  doubles : array[0..5] of double;

begin
  // Fill up the number array
  doubles[0] :=  25.0;
  doubles[1] := 100.5;
  doubles[2] :=  -4.125;
  doubles[3] :=  75.0;
  doubles[4] := -62.0;
  doubles[5] :=   0.0;

  ShowMessage('Sum of array values = '+FloatToStr(Sum(doubles)));
  ShowMessage('12.3 + 45.6 = '+FloatToStr(Sum([12.3, 45.6])));
end;
 
end.
Hide full unit code
   Sum of array values = 134.375
   12.3 + 45.6 = 57.9