Function definition in Julia

207 Views Asked by At

Does Julia specifically has a function definition? If yes what is the BNF for it?

For example it has a function declaration and function call with a BNF

•Function Declaration 
function name ( arguments :: type ) 
      #expressions 
End 

<function> → (function <identifier> ( <arguments> ) <expressionList> end) | 
<identifier>( <arguments> ) <expressionList> end 
<arguments> → <identifier> :: <type> | (<identifier> :: <type>),arguments>|e 

•Function Call 
      x = sum (12 , y :: Int32 ) 

 <funcall> → <identifier> = <identifier> ( <parameterList> ) 
 <parameterList> → <parameter> :: <type>, < parameterList> | <parameter> ::<type> | < parameter >, <parameterList> 
 <parameter> → <identifier> | <element> | e
1

There are 1 best solutions below

1
On

As Matt B. mentioned in the comments, the Julia syntax is not context-free.

If <...> is a valid function call, then in general the following are valid method definitions:

function <...>; (body); end
<...> = (body)

Furthermore, it is allowed to add a return type annotation to the function call:

function <...>::ReturnType; (body); end
<...>::ReturnType = (body)

Any number of where clauses is also allowed, either instead or in addition to the return type, for both short and long forms:

function <...>::ReturnType where T; (body); end
(<...>::ReturnType) where S = (body)
function <...> where T where S; (body); end
<...> where {S, T} = (body)

Both short and long forms support certain qualifiers:

global function <...>; (body); end
local <...> = (body)

Note that the function call itself can come in several forms; for example each of these are valid:

x ← y = x + y
function (x ← y)::Int; 10; end