Call a function: Difference between revisions

No edit summary
(19 intermediate revisions by 10 users not shown)
Line 840:
<b>Subroutines</b> are provided for compatibility with older, unstructured dialects of BASIC; otherwise they are never really used. They require statements to be numbered, and they can neither receive arguments nor return values: they can only manipulate global variables. The <tt>GOSUB</tt> and <tt>RETURN</tt> statements in fact mirror assembly language 'jump to subroutine' and 'return from subroutine' instructions quite closely.
<syntaxhighlight lang="bbcbasic">200 GOSUB 30050</syntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
Function calling is the sole primitive in the Lambda Calculus. The application of function f on argument a is denoted 01 f a in Binary Lambda Calculus. Multi argument functions are achieved by currying, i.e. a function of the first argument returns a function of the 2nd argument, etc. A good example is the Church numeral 2, which given a function f and an argument x, applies f twice on x: C2 = \f. \x. f (f x). This is written in BLC as
 
<pre>00 00 01 110 01 110 01</pre>
 
=={{header|BQN}}==
 
Line 1,809 ⟶ 1,816:
 
=={{header|EasyLang}}==
<syntaxhighlight>
EasyLang distinguishes between subroutines and functions.
func sqr n .
<syntaxhighlight lang="easylang">call subroutine # call a subroutine
return n * n
call function1 result # call a function with no arguments, with return value
.
call function2 arg1 arg2 # call a function with arguments, no return value
print sqr 3
call function3 arg1 arg2 result # call a function with arguments and return value</syntaxhighlight>
#
proc divmod a b . q r .
q = a div b
r = a mod b
.
divmod 11 3 q r
print q & " " & r
#
subr sqr2
a = a * a
.
a = 5
sqr2
print a
</syntaxhighlight>
 
=={{header|Ecstasy}}==
Line 1,830 ⟶ 1,852:
<b><i>Calling a function with optional arguments:</i></b>
<syntaxhighlight lang="java">
module CallOptArgsFunc {
static Int foo(Int a=0, Int b=99, Int c=-1) {
{
static Int foo(Int a=0, Int b=99, Int c=-1)
{
return a + b + c;
}
 
void run() {
{
@Inject Console console;
console.printlnprint($"{foo() == {foo()}");
console.printlnprint($"{foo(1) == {foo(1)}");
console.printlnprint($"{foo(1, 2) == {foo(1, 2)}");
console.printlnprint($"{foo(1, 2, 3) == {foo(1, 2, 3)}");
}
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
foo() == 98
foo(1) == 99
foo(1, 2) == 2
foo(1, 2, 3) == 6
</syntaxhighlight>
 
{{out}}
<pre>
foo()=98
foo(1)=99
foo(1, 2)=2
foo(1, 2, 3)=6
</pre>
 
<b><i>Calling a function with a variable number of arguments:</i></b>
<syntaxhighlight lang="java">
module CallVarArgsFunc {
{
// Ecstasy does not have a var-args concept; instead, array notation is used
static Int foo(Int[] args = []) {
{
return args.size;
}
 
void run() {
{
@Inject Console console;
console.printlnprint($"{foo() == {foo()}");
console.printlnprint($"{foo([]) == {foo([])}");
console.printlnprint($"{foo([1]) == {foo([1])}");
console.printlnprint($"{foo([1, 2]) == {foo([1, 2])}");
console.printlnprint($"{foo([1, 2, 3]) == {foo([1, 2, 3])}");
}
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
foo() == 0
foo([]) == 0
foo([1]) == 1
foo([1, 2]) == 2
foo([1, 2, 3]) == 3
</syntaxhighlight>
 
{{out}}
<pre>
foo()=0
foo([])=0
foo([1])=1
foo([1, 2])=2
foo([1, 2, 3])=3
</pre>
 
<b><i>Calling a function with named arguments:</i></b>
<syntaxhighlight lang="java">
module CallNamedArgsFunc {
static String foo(Int a=1, Int b=2, Int c=3) {
{
static String foo(Int a=1, Int b=2, Int c=3)
{
return $"a:{a}, b:{b}, c:{c}";
}
 
void run() {
{
@Inject Console console;
console.printlnprint($"{foo(c=9, b=8, a=7) == {foo(c=9, b=8, a=7)}");
console.printlnprint($"{foo(4, c=6, b=5) == {foo(4, c=6, b=5)}");
console.printlnprint($"{foo(c=99) == {foo(c=99)}");
}
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
foo(c=9, b=8, a=7) == a:7, b:8, c:9
foo(4, c=6, b=5) == a:4, b:5, c:6
foo(c=99) == a:1, b:2, c:99
</syntaxhighlight>
 
{{out}}
<pre>
foo(c=9, b=8, a=7)=a:7, b:8, c:9
foo(4, c=6, b=5)=a:4, b:5, c:6
foo(c=99)=a:1, b:2, c:99
</pre>
 
<b><i>Using a function in first-class context within an expression:</i></b> Functions are always first class in Ecstasy; everything (including classes, types, methods, properties, functions, variables, etc.) is an object.
<syntaxhighlight lang="java">
module FirstClassFunctions {
{
@Inject Console console;
void run() {
{
function Int(String) stringLen = s -> s.size;
function Int(Int, Int) sum = (n1, n2) -> n1+n2;
String[] testData = ["abc", "easy", "as", "123"];
console.printlnprint($|total string length of values in {testData} =\
| {testData.map(stringLen).reduce(0, sum)}
);
}
}
}
</syntaxhighlight>
 
Output:
{{out}}
<syntaxhighlight>
<pre>
total string length of values in [abc, easy, as, 123] = 12
</pre>
</syntaxhighlight>
 
<b><i>Obtaining the return value of a function:</i></b>
<syntaxhighlight lang="java">
module ObtainReturnValues {
(Int, String, Dec) foo() {
{
(Int, String, Dec) foo()
{
return 3, "hello!", 9.87;
}
 
void run() {
{
foo(); // ignore return values
Int i1 = foo(); // only use first returned value
Line 1,949 ⟶ 1,961:
 
@Inject Console console;
console.printlnprint($"{i3={i3}, {s3={s3}, {d3={d3}, {t={t}");
}
}
}
</syntaxhighlight>
 
Output:
{{out}}
<syntaxhighlight>
<pre>
i3=3, s3=hello!, d3=9.87, t=(3, hello!, 9.87)
</pre>
</syntaxhighlight>
 
<b><i>Distinguishing built-in functions and user-defined functions:</i></b>
Line 1,962 ⟶ 1,975:
// Ecstasy does not have any built-in functions. However, there are two keywords
// ("is" and "as") that use a function-like syntax:
module IsAndAs {
Int|String foo() {
Int|String foo()
{
return "hello";
}
 
void run() {
{
@Inject Console console;
Object o = foo();
if (o.is(String)) { // <- looks like a function call
{
String s = o.as(String); // <- looks like a function call
console.printlnprint($"foo returned the string: {s.quoted()}");
}
}
}
}
</syntaxhighlight>
 
Output:
{{out}}
<syntaxhighlight>
<pre>
foo returned the string: "hello"
</pre>
</syntaxhighlight>
 
<b><i>Distinguishing subroutines and functions:</i></b> There is no such thing as a subroutine in Ecstasy. There are only methods (virtual functions with a "this"), functions, and object constructors.
Line 1,992 ⟶ 2,002:
<b><i>Is partial application possible and how:</i></b>
<syntaxhighlight lang="java">
module PartialApplication {
void foo(String s, Int i, Dec d) {
{
void foo(String s, Int i, Dec d)
{
@Inject Console console;
console.printlnprint($"inside call to foo({s=}, {i=}, {d=})");
}
 
void run() {
{
// note that the "&" obtains the reference to the function, and suppresses the
// invocation thereof, so it is *allowed* in all three of these cases, but it
Line 2,012 ⟶ 2,019:
partBound("hello", 2.718);
allBound();
}
}
}
</syntaxhighlight>
 
Output:
{{out}}
<syntaxhighlight>
<pre>
inside call to foo(nothing, 0, 0)
inside call to foo(hellos=nothing, 99i=0, 2.718d=0)
inside call to foo(worlds=hello, i=99, 3d=2.14718)
inside call to foo(s=world, i=99, d=3.14)
</syntaxhighlight>
</pre>
 
=={{header|Elena}}==
Line 2,105 ⟶ 2,113:
</syntaxhighlight>
 
==={{header|EMal}}===
<syntaxhighlight lang="emal">
# an utility function to better solve the task
fun task = void by text about, fun code
writeLine(0U00b7 + " " + about)
Line 3,054 ⟶ 3,061:
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}}==
<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.
 
Line 3,126 ⟶ 3,235:
* Is partial application possible and how
Don't know
 
=={{header|JavaScript}}==
 
Line 3,199 ⟶ 3,309:
'''Using a function in statement context'''
 
The assignment to a local variable (e.g. <tt>(2*2) as $two</tt>) is similar to a statement context in that the expression as a whole does nothing to the flow of values from its input to its output.
 
'''Using a function in first-class context within an expression'''
Line 3,225 ⟶ 3,335:
 
See [[Currying#jq]].
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
Line 3,448 ⟶ 3,559:
 
 
# In langLang there are text and array varags parameters
fp.varArgsText = ($text...) -> \!
fp.varArgsText(1) # Var args text will be called with "1"
Line 3,486 ⟶ 3,597:
$ret = fp.inc2(40) # $ret is 42
 
# Built-in (They are called predefined functions in langLang) start with the "func." or "fn." prefix wheras user-defined functions start with "fp."
# Linker functions start with "linker." or "ln."
# Predefined and linker functions can be stored in a user-defined function
Line 3,492 ⟶ 3,603:
fp.userDefinedFunc(Called println)
 
# In langLang there are no subroutines
 
# In langLang functions can have call-by-pointer values
# $ptr is a pointer to the called value
fp.callByPtr = ($[ptr]) -> \!
Line 3,581 ⟶ 3,692:
 
<syntaxhighlight lang="langur">val .sum = foldfrom(
ffn(.sum, .i, .c) .sum + toNumbernumber(.c, 36) x* .weight[.i],
0,
pseries len .code,
split ZLS, .code,
)
# split, pseries, and len using unbounded lists, ending before comma preceding line return</syntaxhighlight>
Line 3,592 ⟶ 3,703:
}
# unbounded list on keys bounded by closing parenthesis of sort</syntaxhighlight>
 
=={{header|Latitude}}==
 
Line 3,724 ⟶ 3,836:
 
* There is no distinction made in LFE/Erlang between functions that are built-in and those that are not.
* "Built-in" for LFE/Erlang usually can be figured out: if a function has the module name <code>erlang</code>, e.g., <code>(: erlang list_to_integer ... )</codcode>, then it's built-in.
* Most of the functions that come with LFE/Erlang are not even in the <code>erlang</code> module, but exist in other modules (e.g., <code>io</code>, <code>math</code>, etc.) and in OTP.
* One uses user/third-party modules in exactly the same way as one uses built-ins and modules that come with the Erlang distribution.
Line 3,744 ⟶ 3,856:
* Not explicitly.
* However, one can use <code>lambda</code>s to achieve the same effect.
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
Line 6,014 ⟶ 6,127:
macro definition is responsible for evaluating what it needs to. But macros likely
fall into a different category than the scope of this task.
 
=={{header|SmallBASIC}}==
<syntaxhighlight lang="basic">
func F1()
return 1
end
 
func F2(a)
return a + 1
end
 
func F3(a, b)
return a + b
end
 
func F4(byref a)
a = 5
return a + 1
end
 
sub S1(a, b)
print a, b
end
 
sub S2(byref a)
a = 5
end
 
var1 = 1
var2 = 2
 
' Functions return a result and return-value must be assigned to a variable
result = F1()
result = F2(var1)
result = F3(var1, var2)
' Parameters are passed by reference if byref is used in function definition
result = F4(var1) ' result = 6 and var1 = 5
 
' Subroutines can't return a result
S1(var1, var2)
' Parameters are passed by reference if byref is used in sub definition.
' This can be used to return a result indirectly
S2(var1) ' var1 = 5
 
' Functions and subroutines can take expressions as parameter
result = F2(1 + 2)
</syntaxhighlight>
 
 
=={{header|Smalltalk}}==
Line 6,347 ⟶ 6,508:
Here are some examples:
 
<syntaxhighlight lang="ecmascriptwren">var f1 = Fn.new { System.print("Function 'f1' with no arguments called.") }
var f2 = Fn.new { |a, b|
System.print("Function 'f2' with 2 arguments called and passed %(a) & %(b).")
Line 6,391 ⟶ 6,552:
50
</pre>
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">; call a function (procedure) with no arguments:
885

edits