Call a function: Difference between revisions
m
→{{header|Ecstasy}}
m (Update Lang example: Fix spelling of Lang) |
|||
Line 1,831:
<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.
console.
console.
console.
}
}
</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.
console.
console.
console.
console.
}
}
</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) {
return $"a:{a}, b:{b}, c:{c}";
void run() {
@Inject Console console;
console.
console.
console.
}
}
</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.
}
}
</syntaxhighlight>
{{out}}
<pre>
total string length of values in [abc, easy, as, 123] = 12
</pre>
<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
Line 1,950 ⟶ 1,940:
@Inject Console console;
console.
}
}
</syntaxhighlight>
{{out}}
<pre>
i3=3, s3=hello!, d3=9.87, t=(3, hello!, 9.87)
</pre>
<b><i>Distinguishing built-in functions and user-defined functions:</i></b>
Line 1,963 ⟶ 1,954:
// 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))
String s = o.as(String); // <- looks like a function call
console.
}
}
}
</syntaxhighlight>
{{out}}
<pre>
foo returned the string: "hello"
</pre>
<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 ⟶ 1,981:
<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.
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,013 ⟶ 1,998:
partBound("hello", 2.718);
allBound();
}
}
</syntaxhighlight>
{{out}}
<pre>
inside call to foo(
inside call to foo(
inside call to foo(s=world, i=99, d=3.14)
</pre>
=={{header|Elena}}==
|