Defined function (traditional): Difference between revisions

Jump to navigation Jump to search
Line 32: Line 32:
Tradfns allow both functional and procedural programming, and are essential for [[object-oriented programming]]. They support a full set of [[keyword]]s for flow control and object declarations.
Tradfns allow both functional and procedural programming, and are essential for [[object-oriented programming]]. They support a full set of [[keyword]]s for flow control and object declarations.


One of the most noticeable differences from dfns is that tradfns must declare their locals, because assignments are global by default. The declaration is done in a header line which also determines the function's name and calling syntax. [[Dyalog APL]] also allows dynamic localisation of names while a tradfn is running.<ref>[[Dyalog Ltd.]] Language Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/shadow.htm Shadow Name].</ref>
One of the most noticeable differences from dfns is that tradfns must declare their locals, because assignments are global by default. The declaration is done in a header line which also determines the function's name and calling syntax. [[Dyalog APL]] adds two extensions to this:


# Dynamic localisation of names while a tradfn is running.<ref>[[Dyalog Ltd.]] Language Reference Guide. [https://help.dyalog.com/latest/#Language/System%20Functions/shadow.htm Shadow Name].</ref>
# The declaration of additional local names on separate lines following the header line.<ref>[[Dyalog Ltd.]] Programming Reference Guide.  [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/Locals%20Lines.htm Locals Lines].</ref>
Tradfns cannot be nested but ''can'' contain dfns. A tradfn can dynamically create another tradfn by [[Fix|"fixing"]] its source, and if the function thus created has a name which has been localised, the inner function will only exist the scope of the outer function. Nested functions are less necessary due to tradfns using [[wikipedia:dynamic scoping|dynamic scoping]] as opposed to the lexical scoping of dfns. In other words, a tradfn can "see" locals of its caller.
Tradfns cannot be nested but ''can'' contain dfns. A tradfn can dynamically create another tradfn by [[Fix|"fixing"]] its source, and if the function thus created has a name which has been localised, the inner function will only exist the scope of the outer function. Nested functions are less necessary due to tradfns using [[wikipedia:dynamic scoping|dynamic scoping]] as opposed to the lexical scoping of dfns. In other words, a tradfn can "see" locals of its caller.


A tradfn can be [[niladic function|niladic]] which causes it to behave syntactically like an array. However, every time its name is referenced, it will run to create a result (if any). Such methods are often used to return a cache or as an entry point for the user.
A tradfn can be [[niladic function|niladic]] which causes it to behave syntactically like an array. However, every time its name is referenced, it will run to create a result (if any). Such methods are often used to return a cache or as an entry point for the user.


Some dialects have let the header syntax explicitly specify that a function is [[ambivalent]] by enclosing the left argument name in curly braces (for example <source lang=apl inline>result←{left} Function right</source>) while others treat all functions that specify a left argument name as ambivalent and leave it up to the programmer to check for the presence of a value.
=== Dyalog APL extensions ===
In addition to the two above-mentioned extensions for localising names, Dyalog APL adds a few features to tradfn.<ref>[[Dyalog Ltd.]] Programming Reference Guide. [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/Model%20Syntax.htm Model Syntax].</ref>


Some dialects allow a tradfn to return a function value as result. The returned function will replace the function and its arguments (if any) in the calling expression:
The the header syntax can explicitly specify that a function is [[ambivalent]] by enclosing the left argument name in curly braces (for example <source lang=apl inline>result←{left} Function right</source>). Others dialects treat all functions that declare a left argument name as ambivalent and leave it up to the programmer to check for the presence of a value.
 
A tradfn can return a function value as result. The returned function will replace the function and its arguments (if any) in the calling expression:
<source lang=apl>
<source lang=apl>
     ∇  Fn←Apply name       
     ∇  Fn←Apply name       
Line 54: Line 59:
12
12
</source>
</source>
A tradfn can be declared as shy, that is, it exhibits the same behaviour as an assignment, in that by default, no result is printed when the function terminates, but attempting to use the result still succeeds. This declaration is done by putting curly braces around the result name (for example <source lang=apl inline>{result}←left Function right</source>).
The right argument and the result can be a name list instead of single name. The interpreter will unpack the right argument when the function is called, and collect the result when the function returns.<ref>[[Dyalog Ltd.]] Programming Reference Guide.  [https://help.dyalog.com/latest/#Language/Defined%20Functions%20and%20Operators/Namelists.htm Namelists].</ref>


== A+ ==
== A+ ==

Navigation menu