Call a function: Difference between revisions

add bqn
(Undo revision 350834 by Razetime (talk))
(add bqn)
Line 789:
<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.
<lang bbcbasic>200 GOSUB 30050</lang>
 
 
 
=={{header|BQN}}==
 
BQN's functions take one or two arguments, and the behave like the primitive functions of the language.
 
'''Calling a function that requires no arguments:''' BQN does not have zero argument functions. Having a function that does so is done with the help of a dummy argument, like so:
 
<lang BQN>{𝕊 ·: 1 + 1}0</lang>
 
The dot symbol <code>·</code> indicates that the argument is nothing, and hence is discarded. Hence, the zero provided to it is discarded, and 1 + 1 = 2 is returned.
 
'''Calling a function with a fixed number of arguments:''' BQN functions always take 1 or two arguments, and their names must always start with a capital letter. A function is called like a primitive function, by writing its name or the function itself between the arguments. For example, given a function named <code>F</code>:
 
<lang bqn>F 1</lang> is an example of a single argument call.
 
<lang bqn>2 F 1</lang> is an example of a two argument call.
 
'''Calling a function with optional arguments:''' optional arguments are not supported by BQN.
 
'''Calling a function with a variable number of arguments:''' A function supporting variable arguments can be made by taking an array as any of the arguments.
 
'''Calling a function with named arguments:''' BQN has block headers, which destructure an input array into given variables using pattern matching. These can then be referenced later by the names given.
 
<lang bqn>{
𝕊 one‿two‿three:
one∾two∾three
}</lang>
 
Given a three element array, the above example will concatenate them all together.
 
'''Using a function in statement context:''' BQN user defined functions have the same syntactic roles as primitive functions in an expression, so they can be used like any primitive.
 
<lang bqn>1 {𝕨+𝕩} 2</lang>
is the same as
<lang bqn>1 + 2</lang>
 
'''Using a function in first-class context within an expression:''' BQN supports lisp-style functional programming, and hence supports first class usage of functions.
 
<lang bqn>⟨+, -, ∾⟩</lang>
is an example of a list of functions, which can later be called with the help of a higher order function.
 
'''Obtaining the return value of a function:''' A block function will always return the value of the last statement within it. To obtain the return value of a function, you can assign it to a variable, or modify an existing variable with the return value.
 
<lang bqn>var ← Func # insert arg here</lang>
 
Arguments are passed to BQN functions by value only.
 
'''Partial Application:''' BQN has two combinators for this purpose. Before(<code>⊸</code>) returns a function with a constant left argument, and After(<code>⟜</code>) returns a function with a constant right argument.
 
<lang bqn>+⟜2</lang> will add two to the number given to it.
<lang bqn>2⊸-</lang> will subtract its input from two.
 
=={{header|Bracmat}}==
236

edits