Call a function: Difference between revisions

(10 intermediate revisions by 6 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 lang="easylang">
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 arguments
print sqr 3
call someproc2 arg1 arg2 res # call a procedure with in-arguments and 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 3,288 ⟶ 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,314 ⟶ 3,335:
 
See [[Currying#jq]].
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
Line 3,670 ⟶ 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,681 ⟶ 3,703:
}
# unbounded list on keys bounded by closing parenthesis of sort</syntaxhighlight>
 
=={{header|Latitude}}==
 
Line 3,813 ⟶ 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,833 ⟶ 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,103 ⟶ 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,436 ⟶ 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,480 ⟶ 6,552:
50
</pre>
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">; call a function (procedure) with no arguments:
885

edits