Call a function: Difference between revisions

Line 13:
 
Note this task is about methods of calling functions, not methods of [[Function definition|defining functions]], which is a separate topic.
 
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon have generalized procedures and syntax that are used to implement functions, subroutines and generators.
* Procedures can return values or not and callers may use the returned values or not.
* Procedures in Icon and Unicon are first class values and can be assigned to variables which can then be used to call procedures. This also facilitates some additional calling syntax.
* Additionally, co-expressions are supported which allow for co-routine like transfers of control between two or more procedures. There are some differences in syntax for co-expression calls.
* There are no differences between calling built-in vs. user defined functions
* Named arguments is not natively supported; however, they can be supported using a user defined procedure as shown in [[Named_parameters#Icon_and_Unicon|Named parameters]]
* Method calling is similar with some extended syntax
* Arguments are basically passed by value or reference based on their type. Immutable values like strings, and numbers are passed by value. Mutable data types like structures are essentially references and although these are passed by value the effective behavior is like a call by reference.
 
For more information see [[Icon%2BUnicon/Intro|Icon and Unicon Introduction on Rosetta]]
 
<lang Icon>procedure main() # demonstrate and describe function calling syntax and semantics
 
# normal procedure/function calling
 
f() # no arguments, also command context
f(x) # fixed number of arguments
f(x,h,w) # variable number of arguments (varargs)
y := f(x) # Obtaining the returned value of a function
# procedures as first class values and string invocation
 
f!L # Alternate calling syntax using a list as args
(if \x then f else g)() # call (f or g)()
f := write # assign a procedure
f("Write is now called") # ... and call it
"f"() # string invocation, procedure
"-"(1) # string invocation, operator
 
# Co-expressions
f{e1,e2} # parallel evaluation co-expression call
# equivalent to f([create e1, create e2])
expr @ coexp # transmission of a single value to a coexpression
[e1,e2]@coexp # ... of multiple values (list) to a coexpression
coexp(e1,e2) # ... same as above but only in Unicon
 
# Other
f("x:=",1,"y:=",2) # named parameters (user defined)
end</lang>
 
=={{header|J}}==
Anonymous user