Call a function

Revision as of 16:50, 27 December 2021 by Razetime (talk | contribs) (add bqn)

BBC BASIC

BBC BASIC distinguishes between functions (which return one value), procedures (which may return an arbitrary number of values including zero), and subroutines. Functions can be built-in or user-defined. A call to a built-in function (for example, the square root function) is an expression: <lang bbcbasic>PRINT SQR(2)</lang> The parentheses can often be omitted: <lang bbcbasic>PRINT SQR 2</lang> The name of a user-defined function must begin with FN. A call to it is also an expression: <lang bbcbasic>PRINT FN_foo(bar$, baz%)</lang> (The sigils $ and % identify the variables' types.) A function that takes no arguments can be called omitting the parentheses: <lang bbcbasic>PRINT FN_foo</lang> The name of a procedure must begin with PROC. A call to it is a statement, not an expression: <lang bbcbasic>PROC_foo</lang> If it has arguments, they come in parentheses just as with a function: <lang bbcbasic>PROC_foo(bar$, baz%, quux)</lang> Note that you cannot tell from this syntax which of the variables bar$, baz%, and quux are arguments provided to the procedure and which of them are return values from it. You have to look at where it is defined: <lang bbcbasic>DEF PROC_foo(a$, RETURN b%, RETURN c)</lang> Subroutines 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 GOSUB and RETURN statements in fact mirror assembly language 'jump to subroutine' and 'return from subroutine' instructions quite closely. <lang bbcbasic>200 GOSUB 30050</lang>

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 · 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 F:

<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() returns a function with a constant left argument, and After() 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.