<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/xdmp.function.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>xdmp.function</api:function-name><api:suggest>xdmp.function</api:suggest><api:suggest>xdmp</api:suggest><api:suggest>function</api:suggest><api:function-link mode="xquery" fullname="xdmp:function">/apidoc/8.0/xdmp:function.xml</api:function-link><api:function mode="javascript" name="function" type="builtin" lib="xdmp" category="Extension" subcategory="Function Values" hidden="false" bucket="MarkLogic Built-In Functions" prefix="xdmp" namespace="http://marklogic.com/xdmp" fullname="xdmp.function"><api:summary>
  Returns a function value as an  
  <code class="javascript" xmlns="http://www.w3.org/1999/xhtml">xdmp.function</code> type. 
  You can return an  
  <code class="javascript" xmlns="http://www.w3.org/1999/xhtml">xdmp.function</code> from an expression or
  a function.  You can execute the function referred to by an
   
  <code class="javascript" xmlns="http://www.w3.org/1999/xhtml">xdmp.function</code>
  by passing the  
   <code class="javascript" xmlns="http://www.w3.org/1999/xhtml">xdmp.function</code> value to
  <code class="javascript" xmlns="http://www.w3.org/1999/xhtml">xdmp.apply</code>. If the module-path ends with a file 
  extension matching the ones configured for 
  <code xmlns="http://www.w3.org/1999/xhtml">application/vnd.marklogic-javascript</code> in your
  MarkLogic Mimetypes configuration, and the function's namespace URI is
  empty, the module is considered to be JavaScript.  In this case, the function
  parameter can be empty.
</api:summary><api:params><api:param name="function" type="xs:QName?"><api:param-description>
    The function QName, which includes its local name and namespace. If the
    function is not found in the current query context or in the module
    specified in the second parameter, then an exception is thrown when
    the function is used with <code class="javascript" xmlns="http://www.w3.org/1999/xhtml">xdmp.apply</code>.
  </api:param-description><api:param-name>function</api:param-name><api:param-type>xs.QName?</api:param-type></api:param><api:param name="module-path" type="xs:string?" optional="true"><api:param-description>
    The optional path to the module where the function specified in the
    first parameter is defined.  If the module-path is not supplied, the
    function QName must be in-scope in the query context.  If the empty
    sequence is supplied, the function behaves as if the parameter is not
    supplied (that is, it uses the in-scope query context).
  </api:param-description><api:param-name>module-path</api:param-name><api:param-type>String?</api:param-type></api:param></api:params><api:return>function</api:return><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
xdmp.function(xs.QName("fn:empty"))
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
var f = xdmp.function(xs.QName("fn:concat"));
xdmp.apply(f, "hello", " world");

=&gt; hello world
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// Given square.sjs in the modules database as the following:

module.exports = function(width) {
  return {
    area: function() {
      return width * width;
    },
    set: function(_width) {
      width = _width;
    }
  };
}
// then you can run the following
var mySquare = xdmp.apply(xdmp.function(null,"square.sjs"),4);
xdmp.apply(mySquare.set, 3);
xdmp.apply(mySquare.area)

=&gt; 9
</pre></api:example></api:function></api:function-page>