Call a function: Difference between revisions

Content added Content deleted
m (Update Lang example: Fix spelling of Lang)
Line 1,831: Line 1,831:
<b><i>Calling a function with optional arguments:</i></b>
<b><i>Calling a function with optional arguments:</i></b>
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
module CallOptArgsFunc
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;
return a + b + c;
}
}


void run()
void run() {
{
@Inject Console console;
@Inject Console console;
console.println($"foo() == {foo()}");
console.print($"{foo()=}");
console.println($"foo(1) == {foo(1)}");
console.print($"{foo(1)=}");
console.println($"foo(1, 2) == {foo(1, 2)}");
console.print($"{foo(1, 2)=}");
console.println($"foo(1, 2, 3) == {foo(1, 2, 3)}");
console.print($"{foo(1, 2, 3)=}");
}
}
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
foo() == 98
foo(1) == 99
foo(1, 2) == 2
foo(1, 2, 3) == 6
</syntaxhighlight>
</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>
<b><i>Calling a function with a variable number of arguments:</i></b>
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
module CallVarArgsFunc
module CallVarArgsFunc {
{
// Ecstasy does not have a var-args concept; instead, array notation is used
// Ecstasy does not have a var-args concept; instead, array notation is used
static Int foo(Int[] args = [])
static Int foo(Int[] args = []) {
{
return args.size;
return args.size;
}
}


void run()
void run() {
{
@Inject Console console;
@Inject Console console;
console.println($"foo() == {foo()}");
console.print($"{foo()=}");
console.println($"foo([]) == {foo([])}");
console.print($"{foo([])=}");
console.println($"foo([1]) == {foo([1])}");
console.print($"{foo([1])=}");
console.println($"foo([1, 2]) == {foo([1, 2])}");
console.print($"{foo([1, 2])=}");
console.println($"foo([1, 2, 3]) == {foo([1, 2, 3])}");
console.print($"{foo([1, 2, 3])=}");
}
}
}
}
</syntaxhighlight>
Output:
<syntaxhighlight>
foo() == 0
foo([]) == 0
foo([1]) == 1
foo([1, 2]) == 2
foo([1, 2, 3]) == 3
</syntaxhighlight>
</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>
<b><i>Calling a function with named arguments:</i></b>
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
module CallNamedArgsFunc
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}";
return $"a:{a}, b:{b}, c:{c}";
}
}


void run()
void run() {
{
@Inject Console console;
@Inject Console console;
console.println($"foo(c=9, b=8, a=7) == {foo(c=9, b=8, a=7)}");
console.print($"{foo(c=9, b=8, a=7)=}");
console.println($"foo(4, c=6, b=5) == {foo(4, c=6, b=5)}");
console.print($"{foo(4, c=6, b=5)=}");
console.println($"foo(c=99) == {foo(c=99)}");
console.print($"{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>
</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.
<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">
<syntaxhighlight lang="java">
module FirstClassFunctions
module FirstClassFunctions {
{
@Inject Console console;
@Inject Console console;
void run()
void run() {
{
function Int(String) stringLen = s -> s.size;
function Int(String) stringLen = s -> s.size;
function Int(Int, Int) sum = (n1, n2) -> n1+n2;
function Int(Int, Int) sum = (n1, n2) -> n1+n2;
String[] testData = ["abc", "easy", "as", "123"];
String[] testData = ["abc", "easy", "as", "123"];
console.println($|total string length of values in {testData} =\
console.print($|total string length of values in {testData} =\
| {testData.map(stringLen).reduce(0, sum)}
| {testData.map(stringLen).reduce(0, sum)}
);
);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Output:
{{out}}
<syntaxhighlight>
<pre>
total string length of values in [abc, easy, as, 123] = 12
total string length of values in [abc, easy, as, 123] = 12
</pre>
</syntaxhighlight>


<b><i>Obtaining the return value of a function:</i></b>
<b><i>Obtaining the return value of a function:</i></b>
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
module ObtainReturnValues
module ObtainReturnValues {
(Int, String, Dec) foo() {
{
(Int, String, Dec) foo()
{
return 3, "hello!", 9.87;
return 3, "hello!", 9.87;
}
}


void run()
void run() {
{
foo(); // ignore return values
foo(); // ignore return values
Int i1 = foo(); // only use first returned value
Int i1 = foo(); // only use first returned value
Line 1,950: Line 1,940:


@Inject Console console;
@Inject Console console;
console.println($"i3={i3}, s3={s3}, d3={d3}, t={t}");
console.print($"{i3=}, {s3=}, {d3=}, {t=}");
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Output:
{{out}}
<syntaxhighlight>
<pre>
i3=3, s3=hello!, d3=9.87, t=(3, hello!, 9.87)
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>
<b><i>Distinguishing built-in functions and user-defined functions:</i></b>
Line 1,963: Line 1,954:
// Ecstasy does not have any built-in functions. However, there are two keywords
// Ecstasy does not have any built-in functions. However, there are two keywords
// ("is" and "as") that use a function-like syntax:
// ("is" and "as") that use a function-like syntax:
module IsAndAs
module IsAndAs {
{
Int|String foo() {
Int|String foo()
{
return "hello";
return "hello";
}
}


void run()
void run() {
{
@Inject Console console;
@Inject Console console;
Object o = foo();
Object o = foo();
if (o.is(String)) // <- looks like a function call
if (o.is(String)) { // <- looks like a function call
{
String s = o.as(String); // <- looks like a function call
String s = o.as(String); // <- looks like a function call
console.println($"foo returned the string: {s.quoted()}");
console.print($"foo returned the string: {s.quoted()}");
}
}
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Output:
{{out}}
<syntaxhighlight>
<pre>
foo returned the string: "hello"
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.
<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,993: Line 1,981:
<b><i>Is partial application possible and how:</i></b>
<b><i>Is partial application possible and how:</i></b>
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
module PartialApplication
module PartialApplication {
void foo(String s, Int i, Dec d) {
{
void foo(String s, Int i, Dec d)
{
@Inject Console console;
@Inject Console console;
console.println($"inside call to foo({s}, {i}, {d})");
console.print($"inside call to foo({s=}, {i=}, {d=})");
}
}


void run()
void run() {
{
// note that the "&" obtains the reference to the function, and suppresses the
// 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
// invocation thereof, so it is *allowed* in all three of these cases, but it
Line 2,013: Line 1,998:
partBound("hello", 2.718);
partBound("hello", 2.718);
allBound();
allBound();
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Output:
{{out}}
<syntaxhighlight>
<pre>
inside call to foo(nothing, 0, 0)
inside call to foo(hello, 99, 2.718)
inside call to foo(s=nothing, i=0, d=0)
inside call to foo(world, 99, 3.14)
inside call to foo(s=hello, i=99, d=2.718)
inside call to foo(s=world, i=99, d=3.14)
</syntaxhighlight>
</pre>


=={{header|Elena}}==
=={{header|Elena}}==