Call a function: Difference between revisions
m
→unbounded lists
Langurmonkey (talk | contribs) |
|||
(14 intermediate revisions by 8 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>
func sqr n .
return n * n
.
print sqr 3
#
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>
Line 1,831 ⟶ 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) {
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,961:
@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,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() {
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 ⟶ 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) {
@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 ⟶ 2,019:
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}}==
Line 3,302 ⟶ 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,328 ⟶ 3,335:
See [[Currying#jq]].
=={{header|Julia}}==
<syntaxhighlight lang="julia">
Line 3,551 ⟶ 3,559:
# In
fp.varArgsText = ($text...) -> \!
fp.varArgsText(1) # Var args text will be called with "1"
Line 3,589 ⟶ 3,597:
$ret = fp.inc2(40) # $ret is 42
# Built-in (They are called predefined functions in
# Linker functions start with "linker." or "ln."
# Predefined and linker functions can be stored in a user-defined function
Line 3,595 ⟶ 3,603:
fp.userDefinedFunc(Called println)
# In
# In
# $ptr is a pointer to the called value
fp.callByPtr = ($[ptr]) -> \!
Line 3,684 ⟶ 3,692:
<syntaxhighlight lang="langur">val .sum = foldfrom(
0,
pseries len .code,
split
)
# split, pseries, and len using unbounded lists, ending before comma preceding line return</syntaxhighlight>
Line 3,695 ⟶ 3,703:
}
# unbounded list on keys bounded by closing parenthesis of sort</syntaxhighlight>
=={{header|Latitude}}==
Line 3,827 ⟶ 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 ... )</
* 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,847 ⟶ 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,117 ⟶ 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,450 ⟶ 6,508:
Here are some examples:
<syntaxhighlight lang="
var f2 = Fn.new { |a, b|
System.print("Function 'f2' with 2 arguments called and passed %(a) & %(b).")
Line 6,494 ⟶ 6,552:
50
</pre>
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">; call a function (procedure) with no arguments:
|