Call a function: Difference between revisions

Added Java info
(Added Java info)
Line 426:
 
=={{header|Java}}==
Java does not have functions, but Java classes have "methods" which are equivalent.
 
* Calling a function that requires no arguments
// todo...
<lang java>myMethod()</lang>
We didn't specify an object (or a class) as the location of the method, so <tt>this.myMethod()</tt> is assumed. This applies to all the following examples.
 
* Calling a function with a fixed number of arguments
<lang java>myMethod(97, 3.14)</lang>
 
* Calling a function with optional arguments
This is possible if the method name is overloaded with different argument lists. For example:
<lang java>int myMethod(int a, double b){
// return result of doing sums with a and b
}
 
int myMethod(int a){
return f(a, 1.414);
}</lang>
 
The compiler figures out which method to call based on the types of the arguments, so in this case the second argument appears to be optional. If you omit it, the value <tt>1.414</tt> is used.
<lang java>System.out.println( myMethod( 97, 3.14 ) );
System.out.println( myMethod( 97 ) );</lang>
 
* Calling a function with a variable number of arguments
This is possible if the method is defined with varargs syntax. For example:
<lang java>void printAll(String... strings){
for ( String s : strings )
System.out.println( s );
}</lang>
 
The type of <tt>strings</tt> is actually a string array, but the caller just passes strings:
<lang java>printAll( "Freeman" );
printAll( "Freeman", "Hardy", "Willis" );</lang>
 
To avoid ambiguity, only the last argument to a function can have varargs.
 
* Calling a function with named arguments
Not directly possible, but you could simulate this (somewhat verbosely):
<lang java>int myMethod( Map<String,Object> params ){
return
((Integer)params.get("x")).intValue()
+ ((Integer)params.get("y")).intValue();
}</lang>
 
Called like this:
<lang java>System.out.println( myMethod(new HashMap<String,Object>(){{put("x",27);put("y",52);}}) );</lang>
 
Yuk.
 
* Using a function in statement context
If this means "use a function where a statement is expected", see all the other examples
 
* Using a function in first-class context within an expression
Not possible - must be wrapped in a class
 
* Obtaining the return value of a function
<lang java>int i = myMethod(x);</lang>
 
* Distinguishing built-in functions and user-defined functions
No distinction - all methods belong to classes, and there is no real distinction between built-in and user-defined classes.
 
* Distinguishing subroutines and functions
If the return type is void, you might consider a method as a subroutine rather than a function.
 
* Stating whether arguments are passed by value or by reference
All arguments are passed by value, but since object variables contain a reference to an object (not the object itself), objects appear to be passed by reference. For example:
<lang java>myMethod(List<String> list){
// If I change the contents of the list here, the caller will see the change
}</lang>
 
* Is partial application possible and how
Don't know
 
=={{header|JavaScript}}==
Anonymous user