Function definition: Difference between revisions

m (Dialects of BASIC moved to the BASIC section. QuickBASIC moved to its alphabetical position.)
(One intermediate revision by one other user not shown)
Line 662:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
In ANSI BASIC, functions can be defined as either formulas or multi-line external or internal subroutines. External functions are independent program units that can be called from within the program. Internal functions are considered part of the program unit they are contained in and can only be called from within that unit. External functions do not share any information with other program units and exchange information through parameters and returned values. Internal functions share everything with their surrounding program unit except for their parameters. Internal functions do not have local variables.
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 DEF Multiply(A, B) = A * B
110 DECLARE FUNCTION MultiplyI
120 DECLARE EXTERNAL FUNCTION MultiplyE
130 PRINT Multiply(3, 1.23456)
140 PRINT MultiplyI(3, 1.23456)
150 PRINT MultiplyE(3, 1.23456)
160 FUNCTION MultiplyI(X, Y)
170 LET MultiplyI = X * Y
180 END FUNCTION
190 END
200 EXTERNAL FUNCTION MultiplyE(A, B)
210 LET MultiplyE = A * B
220 END FUNCTION
.multiply(3, 4)</syntaxhighlight>
{{out}}
<pre>
3.70368
3.70368
3.70368
</pre>
 
==={{header|Applesoft BASIC}}===
Line 2,166 ⟶ 2,190:
Parameters are defined within parentheses after the fn token. To specify no parameters, use an empty set of parentheses.
<syntaxhighlight lang="langur">val .multiply = fn(.x, .y) { .x * .y }
.multiply(3, 4)</syntaxhighlight>
 
=== curly braces ===
A function body may use curly braces, but it is not required if it is a single expression.
<syntaxhighlight lang="langur">val .multiply = fn(.x, .y) .x * .y
.multiply(3, 4)</syntaxhighlight>
 
885

edits