Call a function: Difference between revisions

J: more possible interpretations of "named arguments"
(J: more possible interpretations of "named arguments")
Line 25:
''A function with a variable number of arguments (varargs)'': When you need to deal with more than four arguments (or more than two arguments in a context which demands a verb) or when you need to deal with a variable number of arguments, you can pass them in a list. 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 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 be achieved by 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