Call a function: Difference between revisions
Content added Content deleted
Line 3,053: | Line 3,053: | ||
There are no ''differences between calling subroutines and functions'' because J defines neither <code>subroutines</code> nor <code>functions</code>. Instead, J defines <code>verbs</code>, <code>adverbs</code>, and <code>conjunctions</code> which for the purpose of this task are treated as functions. (All of the above examples used verbs. J's adverbs and conjunctions have stronger [[wp:Valence|valence]] than its verbs.) |
There are no ''differences between calling subroutines and functions'' because J defines neither <code>subroutines</code> nor <code>functions</code>. Instead, J defines <code>verbs</code>, <code>adverbs</code>, and <code>conjunctions</code> which for the purpose of this task are treated as functions. (All of the above examples used verbs. J's adverbs and conjunctions have stronger [[wp:Valence|valence]] than its verbs.) |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<kbd><i>"Calling a function that requires no arguments."</i></kbd><br/> |
|||
The parentheses are required. |
|||
<syntaxhighlight lang="java"> |
|||
Object.methodName(); |
|||
</syntaxhighlight> |
|||
<p> |
|||
<kbd><i>"Calling a function with a fixed number of arguments."</i></kbd> |
|||
<syntaxhighlight lang="java"> |
|||
Object.methodName("rosetta", "code"); |
|||
</syntaxhighlight> |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Calling a function with optional arguments."</i></kbd><br/> |
|||
Java doesn't offer the ability to optionalize parameters, although there is something similar.<br/> |
|||
A <kbd>varargs</kbd>, or "Variable Arguments", parameter, could be of 0 length.<br/> |
|||
So if you're only parameter is a <kbd>vararg</kbd> parameter, it's possible to not supply any input. |
|||
This could be viewed, in some situations, as an optional parameter. |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Calling a function with a variable amount of arguments."</i></kbd><br/> |
|||
There is no special syntax, you simply offer the arguments as required. |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Calling a function with named arguments."</i></kbd><br/> |
|||
Java does not offer this feature. |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Using a function in a statement context."</i></kbd><br/> |
|||
Java is not a functional programming language, although Java 8 added basic closures and lambda expressions.<br/> |
|||
They are not in anyway as robust as functional languages like JavaScript.<br/> |
|||
A lambda works specifically with an <code>interface</code> that requires only 1 abstraction.<br/> |
|||
Consider the following <kbd>interface</kbd>. |
|||
<syntaxhighlight lang="java"> |
|||
interface Example { |
|||
int add(int valueA, int valueB); |
|||
} |
|||
</syntaxhighlight> |
|||
You could then implement this interface with a lambda, as opposed to creating an anonymous-class.<br/> |
|||
Consider the following method. |
|||
<syntaxhighlight lang="java"> |
|||
int sum(Example example) { |
|||
return example.add(1, 2); |
|||
} |
|||
</syntaxhighlight> |
|||
You would then provide the closure, or the functionality of the abstraction, during assignment. |
|||
<syntaxhighlight lang="java"> |
|||
Example example = (valueA, valueB) -> valueA + valueB; |
|||
sum(example); |
|||
</syntaxhighlight> |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Using a function in first-class context with an expression."</i></kbd><br /> |
|||
First-class context is out-of-scope for Java, which is statically-typed. |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Obtaining the return value of a function."</i></kbd><br /> |
|||
</p> |
|||
<syntaxhighlight lang="java"> |
|||
String string = Object.methodName("rosetta", "code"); |
|||
</syntaxhighlight> |
|||
<p> |
|||
<kbd><i>"Distinguishing built-in functions and user-defined functions."</i></kbd><br /> |
|||
There is no ambiguity between built-in functions and user-defined functions. |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Distinguishing subroutines and functions."</i></kbd><br /> |
|||
Java refers to all procedures as methods.<br/> |
|||
As with other languages, such as Visual Basic, which uses <kbd>Sub</kbd>, and <kbd>Function</kbd>, |
|||
there is no ambiguity from methods which return values and those that don't. |
|||
</p> |
|||
<p> |
|||
The defining factor is within the method definition.<br/> |
|||
A return-type is declared before the method name, and <code>void</code> is used when there is no returned value. |
|||
<syntaxhighlight lang="java"> |
|||
String methodA(); |
|||
void methodB(); |
|||
</syntaxhighlight> |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Stating whether arguments are passed by value or by reference."</i></kbd><br /> |
|||
The concept of pass-by-value and pass-by-reference is somewhat a redefined measure within Java.<br/> |
|||
For the most part, everything is pass-by-value; there are no pointers and dereferencing, as with C, C++, and Rust.<br/> |
|||
Although, if you're passing an object, it can be viewed as pass-by-reference, since the operation is occurring on the actual object, |
|||
and a new value is not created.<br/> |
|||
Java is essentially an language that was influenced by languages which use pass-by-reference, so it's abstraction is lacking. |
|||
</p> |
|||
<p> |
|||
<kbd><i>"Is partial application possible and how."</i></kbd><br /> |
|||
Not without a closure.<br/> |
|||
I found the following example on [https://en.wikipedia.org/wiki/Partial_application#Implementations Wikipedia - Partial application]. |
|||
</p> |
|||
<syntaxhighlight lang="java"> |
|||
<X, Y, Z> Function<Y, Z> exampleA(BiFunction<X, Y, Z> exampleB, X value) { |
|||
return y -> exampleB.apply(value, y); |
|||
} |
|||
</syntaxhighlight> |
|||
<br /> |
|||
Here is an alternate demonstration.<br /> |
|||
Java does not have functions, but Java classes have "methods" which are equivalent. |
Java does not have functions, but Java classes have "methods" which are equivalent. |
||
Line 3,125: | Line 3,227: | ||
* Is partial application possible and how |
* Is partial application possible and how |
||
Don't know |
Don't know |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||