Differences between functions and procedures in neo4j

Hi,

I ask a question because I got curious while reading the manual.

The difference between functions and procedures in apoc is confusing.

https://neo4j.com/developer/procedures-functions/

  • Functions are simple computations / conversions and return a single value
  • Functions can be used in any expression or predicate
  • Procedures are more complex operations and generate streams of results.
  • Procedures must be used within the CALL clause and YIELD their result columns
  • They can generate, fetch or compute data to make it available to later processing steps in your Cypher query

================================

https://neo4j.com/docs/java-reference/current/extending-neo4j/procedures-and-functions/introduction/

According to those manual, functions in neo4j return only one value, and the cardinality of the main query cannot be increased.

But there is a function(not a procedure) that returns multiple results. allshortestPaths

https://neo4j.com/docs/cypher-manual/current/clauses/match/#all-shortest-paths

What's going on here? Is this just one exception?

It may seem like nothing, but it's a big problem for me that the cardinality of the set can change, so I want to hear the exact answer to this.

Thanks

I agree, the distinction is fairly vague. Pretty much everything you can do with a procedure, you can do with a function. The only real difference is how they can (or are intended to) be used.

Function

Returns a single value, which may be a Map, List, String, or Number. Note, any of these data-types could be SET on a node property.

Procedure

Returns a List (of Nodes, Maps, etc.) that is immediately unwound. Note, a List of nodes, or maps, or lists, could not be SET on a node property.

The distinction really is in convenience of using them in Cypher. Functions can be thrown around pretty much anywhere -- SET a.newval = function(a.oldval, c.otherval) -- while procedures usually provide data to be operated on.

1 Like