Call a function: Difference between revisions

Add Ecstasy example
(Add Ecstasy example)
Line 1,805:
# a function's arity is a property of its behavior and not
# of its definition</syntaxhighlight>
 
=={{header|Ecstasy}}==
<b><i>Calling a function that requires no arguments:</i></b>
<syntaxhighlight lang="java">
foo(); // <-- this is "invoking a function in statement context"
Int x = bar(); // <-- this is "invoking a function in expression context"
</syntaxhighlight>
 
<b><i>Calling a function with a fixed number of arguments:</i></b>
<syntaxhighlight lang="java">
foo(1, 2, 3);
Int x = bar(4, 5, 6);
</syntaxhighlight>
 
<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)
{
return a + b + c;
}
 
void run()
{
@Inject Console console;
console.println($"foo() == {foo()}");
console.println($"foo(1) == {foo(1)}");
console.println($"foo(1, 2) == {foo(1, 2)}");
console.println($"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>
 
<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.println($"foo() == {foo()}");
console.println($"foo([]) == {foo([])}");
console.println($"foo([1]) == {foo([1])}");
console.println($"foo([1, 2]) == {foo([1, 2])}");
console.println($"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>
 
<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)
{
return $"a:{a}, b:{b}, c:{c}";
}
 
void run()
{
@Inject Console console;
console.println($"foo(c=9, b=8, a=7) == {foo(c=9, b=8, a=7)}");
console.println($"foo(4, c=6, b=5) == {foo(4, c=6, b=5)}");
console.println($"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>
 
<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.println($|total string length of values in {testData} =\
| {testData.map(stringLen).reduce(0, sum)}
);
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
total string length of values in [abc, easy, as, 123] = 12
</syntaxhighlight>
 
<b><i>Obtaining the return value of a function:</i></b>
<syntaxhighlight lang="java">
module ObtainReturnValues
{
(Int, String, Dec) foo()
{
return 3, "hello!", 9.87;
}
 
void run()
{
foo(); // ignore return values
Int i1 = foo(); // only use first returned value
(Int i2, String s2) = foo(); // only use first two returned values
(Int i3, String s3, Dec d3) = foo(); // use all returned values
Tuple<Int, String, Dec> t = foo(); // alternatively, get the tuple instead
 
@Inject Console console;
console.println($"i3={i3}, s3={s3}, d3={d3}, t={t}");
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
i3=3, s3=hello!, d3=9.87, t=(3, hello!, 9.87)
</syntaxhighlight>
 
<b><i>Distinguishing built-in functions and user-defined functions:</i></b>
<syntaxhighlight lang="java">
// 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()
{
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.println($"foo returned the string: {s.quoted()}");
}
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
foo returned the string: "hello"
</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.
 
<b><i>Stating whether arguments are passed by value or by reference:</i></b> Ecstasy does not specify whether arguments are passed by value or by reference. However, since all Ecstasy types are <i>conceptually</i> reference types, the behavior is defined as if all arguments are references passed by value; this is the same model used by Java for all of its reference types.
 
<b><i>Is partial application possible and how:</i></b>
<syntaxhighlight lang="java">
module PartialApplication
{
void foo(String s, Int i, Dec d)
{
@Inject Console console;
console.println($"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
// is *required* in the third case:
function void(String, Int, Dec) unbound = foo; // or "foo(_, _, _)"
function void(String, Dec) partBound = unbound(_, 99, _);
function void() allBound = &partBound("world", 3.14);
 
unbound("nothing", 0, 0.0);
partBound("hello", 2.718);
allBound();
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
inside call to foo(nothing, 0, 0)
inside call to foo(hello, 99, 2.718)
inside call to foo(world, 99, 3.14)
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 4.1:
162

edits