Call a function: Difference between revisions

m
 
(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>
EasyLang distinguishes between subroutines and procedures. Procedures can have parameters and have local variables. Subroutines do not. The call syntax is the same.
func sqr n .
<syntaxhighlight lang="easylang">
return n * n
call somesubr # call a subroutine
.
call someproc1 # call a procedure with no parameters
print sqr 3
call someproc2 arg1 arg2 res # call a procedure with arguments, the last one is an inout argument
#
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) {
{
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,950 ⟶ 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,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() {
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,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) {
{
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,013 ⟶ 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 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 langLang there are text and array varags parameters
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 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,595 ⟶ 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,684 ⟶ 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,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 ... )</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,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="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,494 ⟶ 6,552:
50
</pre>
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">; call a function (procedure) with no arguments:
885

edits