Call a function: Difference between revisions

m
Line 136:
If argument types conflict they will need to be put in boxes and the function will have to take its arguments out of the boxes. Here's an unboxed example with five arguments: <lang j> f 1,2,3,4,5</lang> and here's a boxed example with five arguments: <lang j>f (<1),(<2),(<3),(<4),(<5) </lang> Note that the last set of parenthesis is unnecessary <lang j>f (<1),(<2),(<3),(<4),<5</lang> Note also that J offers some syntactic sugar for this kind of list <lang j>f 1; 2; 3; 4; <5</lang>. Note also that if the last argument in a semicolon list is not boxed there is no need to explicitly box it, since that is unambiguous (it must be boxed so that it conforms with the other members of the list). <lang j>f 1; 2; 3; 4; 5</lang>
 
''A function with named arguments'' can be accomplished by calling a function with the names of the arguments. <lang j>f 'george';'tom';'howard'</lang> Other interpretations of this concept are also possible. For example, the leftright argument for a verb might be a list of argument names and the rightleft argument might be a corresponding list of argument values.: <lang j>1 2 3 f 'george';'tom';'howard'</lang> Or, for example a function which requires an object could be thought of as a function with named arguments since an object's members have names:<lang j> obj=: conew'blank'
george__obj=: 1
tom__obj=: 2
howard__obj=: 3
f obj
coerase obj</lang> Name/value pairs can also be used for this purpose and can be implemented in various ways, including passing names followed by values <lang j>f 'george';1;'tom';2;'howard';3</lang> and passing names and values in the same order <lang j>1 2 3 f 'george';'tom';'howard'</lang> and passing a structure of pairs <lang j>f ('george';1),('tom';2),:(howard';3)</lang> Or, for example, the pairs could be individually boxed: <lang j>f ('george';1);('tom';2);<howard';3</lang>
 
''Using a function in command context'' is no different from using a function in any other context, in J. ''Using a function in first class context within an expression'' is no different from using a function in any other context, in J.
6,962

edits