<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/fn.sum.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>fn.sum</api:function-name><api:suggest>fn.sum</api:suggest><api:suggest>fn</api:suggest><api:suggest>sum</api:suggest><api:function-link mode="xquery" fullname="fn:sum">/apidoc/8.0/fn:sum.xml</api:function-link><api:function mode="javascript" name="sum" type="builtin" lib="fn" category="SequenceBuiltins" bucket="W3C-Standard Functions" hidden="false" prefix="fn" namespace="http://www.w3.org/2005/xpath-functions" fullname="fn.sum"><api:summary>
<p xmlns="http://www.w3.org/1999/xhtml">
Returns a value obtained by adding together the values in $arg. If $zero is not
specified, then the value returned for an empty sequence is the xs:integer
value 0. If $zero is specified, then the value returned for an empty
sequence is $zero.
</p><p xmlns="http://www.w3.org/1999/xhtml">
Any values of type xs:untypedAtomic in $arg are cast to xs:double. The items
in the resulting sequence may be reordered in an arbitrary order. The
resulting sequence is referred to below as the converted sequence.
</p><p xmlns="http://www.w3.org/1999/xhtml">
If the converted sequence is empty, then the single-argument form of the
function returns the xs:integer value 0; the two-argument form returns the
value of the argument $zero.
</p><p xmlns="http://www.w3.org/1999/xhtml">
If the converted sequence contains the value NaN, NaN is returned.
</p><p xmlns="http://www.w3.org/1999/xhtml">
All items in $arg must be numeric or derived from a single base type. In
addition, the type must support addition. Duration values must either all be
xs:yearMonthDuration values or must all be xs:dayTimeDuration values. For
numeric values, the numeric promotion rules defined in 6.2 Operators on
Numeric Values are used to promote all values to a single common type. The
sum of a sequence of integers will therefore be an integer, while the sum of
a numeric sequence that includes at least one xs:double will be an
xs:double.
</p><p xmlns="http://www.w3.org/1999/xhtml">
If the above conditions are not met, a type error is raised [err:FORG0006].
</p><p xmlns="http://www.w3.org/1999/xhtml">
Otherwise, the result of the function, using the second signature, is the
result of the expression:
</p><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
   if (fn:count($c) eq 0) then
       $zero
   else if (fn:count($c) eq 1) then
       $c[1]
   else
       $c[1] + fn:sum(subsequence($c, 2))
</pre>
<p xmlns="http://www.w3.org/1999/xhtml">
where $c is the converted sequence.
</p><p xmlns="http://www.w3.org/1999/xhtml">
The result of the function, using the first signature, is the result of the
expression:fn:sum($arg, 0).
</p><p xmlns="http://www.w3.org/1999/xhtml">
For detailed type semantics, see
<a href="http://www.w3.org/TR/xquery-semantics/#sec_fn_aggregates">Section
7.2.10 The fn:min, fn:max, fn:avg, and fn:sum functions[FS]</a>.
</p><p xmlns="http://www.w3.org/1999/xhtml">
Notes:
</p><p xmlns="http://www.w3.org/1999/xhtml">
The second argument allows an appropriate value to be defined to represent the
sum of an empty sequence. For example, when summing a sequence of durations
it would be appropriate to return a zero-length duration of the appropriate
type. This argument is necessary because a system that does dynamic typing
cannot distinguish "an empty sequence of integers", for example, from "an
empty sequence of durations".
</p><p xmlns="http://www.w3.org/1999/xhtml">
If the converted sequence contains exactly one value then that value is
returned.
</p>
</api:summary><api:params><api:param name="arg" type="ValueIterator | Array" class="javascript"><api:param-description>
  The sequence of values to be summed.
  </api:param-description><api:param-name>arg</api:param-name><api:param-type>ValueIterator | Array</api:param-type></api:param><api:param name="zero" type="xs:anyAtomicType?" optional="true"><api:param-description>
   The value to return as zero if the input sequence is the empty sequence.
   This parameter is not available in the 0.9-ml XQuery dialect.
  </api:param-description><api:param-name>zero</api:param-name><api:param-type>xs.anyAtomicType?</api:param-type></api:param></api:params><api:return>xs.anyAtomicType?</api:return><api:usage>
  When running this in the 0.9-ml XQuery dialect, there is no second
  argument to <code xmlns="http://www.w3.org/1999/xhtml">fn:sum</code>; the second ($zero) argument is available
  in both the 1.0 and 1.0-ml dialects.
</api:usage><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
var d1 = xs.yearMonthDuration("P20Y");
var d2 = xs.yearMonthDuration("P10M")
var seq1 = [d1, d2];
fn.sum(seq1);
=&gt;
an xs:yearMonthDuration with a value of 250 months (P20Y10M).

*****
var seq3 = [3, 4, 5];
fn.sum(seq3);
=&gt;
12

*****
fn.sum(null);
=&gt;
0

*****
fn.sum(null, null);
=&gt;
()
</pre></api:example></api:function></api:function-page>