Function definition: Difference between revisions

m
Corrected syntax highlighting of some sections.
(→‎{{header|COBOL}}: Added example using user-defined functions.)
m (Corrected syntax highlighting of some sections.)
Line 1,196:
 
=={{header|PL/I}}==
<lang PL/Ipli>PRODUCT: procedure (a, b) returns (float);
declare (a, b) float;
return (a*b);
Line 1,307:
 
=={{header|R}}==
<lang Rrsplus>mult <- function(a,b) a*b</lang>
In general:
<lang Rrsplus>mult <- function(a,b) {
a*b
# or:
Line 1,317:
=={{header|Racket}}==
A simple function definition that takes 2 arguments.
<lang racketlisp>(define (mutiply a b)
(define (mutiply* a b))</lang>
(* a b))
</lang>
 
Using an explicit <tt>lambda</tt> is completely equivalent:
<lang racketlisp>(define multiply (lambda (a b) (* a b)))</lang>
 
=={{header|Raven}}==
Line 1,467 ⟶ 1,465:
 
=={{header|Standard ML}}==
<lang smlocaml>val multiply = op *</lang>
Equivalently,
<lang smlocaml>fun multiply (x, y) = x * y</lang>
Curried form:
<lang smlocaml>fun multiply x y = x * y</lang>
 
=={{header|Tcl}}==
Line 1,506 ⟶ 1,504:
@(end)
@(multiply 3 4 result)</lang>
<pre>$ txr multiply.txr
result="12"</pre>
In the embedded Lisp dialect, it is possible to write an ordinary function that returns a value:
<lang txr>@(do (defun mult (a b) (* a b))
(format t "3 * 4 = ~a\n" (* 3 4)))</lang>
<pre>$ txr multiply2.txr
3 * 4 = 12</pre>
 
=={{header|UNIX Shell}}==
Note that in the Unix shell, function definitions do not include any argument specifications within the parentheses. Instead arguments to functions are obtained using the positional parameters.
{{works with|Bourne Shell}}
<lang bash>multiply() {
# There is never anything between the parentheses after the function name
# Arguments are obtained using the positional parameters $1, and $2
# The return is given as a parameter to the return command
return `expr "$1" \* "$2"` # The backslash is required to suppress interpolation
}
 
# Call the function
multiply 3 4 # The function is invoked in statement context
echo $? # The dollarhook special variable gives the return value</lang>
{{works with|Bash}}
return an exit code
<lang bash>multiply() {
return $(($1 * $2))
}
 
multiply 5 6
echo $?</lang>
echo the result
<lang bash>multiply() {
echo -n $(($1 * $2))
}
 
echo $(multiply 5 6)</lang>
 
=={{header|Ursala}}==
Functions are declared with an equals sign like constants of any other type.
They may be specified by lambda abstraction, with dummy variables in double quotes, or in point-free form, or any combination. The way multiplication is defined depends on the type of numbers being multiplied. For this example, numbers in standard IEEE double precision are assumed, and the multiply function is defined in terms of the system library function, called using the syntax <code>math..mul</code>.
This is the definition in point free form,
<lang Ursala>multiply = math..mul</lang>
this is the definition using lambda abstraction
<lang Ursala>multiply = ("a","b"). math..mul ("a","b")</lang>
and this is the definition using pattern matching.
<lang Ursala>multiply("a","b") = math..mul ("a","b")</lang>
 
=={{header|V}}==
V uses stack for input arguments and '.' is a word that takes a quote and binds the first word to the sequence of actions supplied in the quote.
<lang v>[multiply *].</lang>
Using it
<lang v>2 3 multiply
=6</lang>
V also allows internal bindings.
<lang v>[multiply
[a b] let
a b *].</lang>
 
=={{header|VBScript}}==
<lang vb>function multiply( multiplicand, multiplier )
multiply = multiplicand * multiplier
end function</lang>
Usage:
<lang vb>dim twosquared
twosquared = multiply(2, 2)</lang>
 
=={{header|Visual Basic .NET}}==
<lang vbnet>Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</lang>
Call the function
<lang vbnet>Multiply(1, 1)</lang>
 
=={{header|Wart}}==
A straightforward way to say how calls of the form <code>(multiply a b)</code> are translated:
<lang python>def (multiply a b)
a*b</lang>
 
<lang python>(multiply 3 4)
=> 12</lang>
 
Functions can also use keyword args.
 
<lang python>(multiply 3 :a 4) # arg order doesn't matter here, but try subtract instead
=> 12</lang>
 
Finally, we can give params better keyword args using <em>aliases</em>:
 
<lang python>def (multiply a b|by)
(* a b)</lang>
 
<lang python>multiply 3 :by 4
=> 12</lang>
 
=={{header|X86 Assembly}}==
X86 Assembly doesn't really have functions. Instead, it has labels that are called. Function arguments can be pushed onto the stack prior to calling or passed to the function in registers.
{{works with|NASM}}
<lang asm>section .text
global _start
 
_multiply_regs:
mul ebx
mov eax, ebx
ret
 
_multiply_stack:
enter 2,0
mov eax, [esp+4]
mov ebx, [esp+8]
mul ebx
mov eax, ebx
leave
ret
 
_start:
mov ax, 6 ;The number to multiply by
mov ebx, 16 ;base number to multiply.
call _multiply_regs
push 6
push 16
call _multiply_stack</lang>
However, in MASM we do have function statements due to the preprocessor.
{{works with|MASM}}
<lang asm>multiply proc arg1:dword, arg2:dword
mov eax, arg1
mov ebx, arg2
mul ebx
mov eax, ebx
ret
multiply endp</lang>
Then to call it.
<lang asm>invoke multiply, 6, 16
;or..
push 16
push 6
call multiply</lang>
Return values are usually put into the register EAX. This, of course is not a must it's simply that it's somewhat of a unofficial standard. For example, C/C++ preprocessors/compilers will translate "return value" into "mov eax, value" followed by the return to caller instruction "ret".
 
=={{header|XPL0}}==
<lang XPL0>func Multiply(A, B); \the characters in parentheses are only a comment
int A, B; \the arguments are actually declared here, as integers
return A*B; \the default (undeclared) function type is integer
\no need to enclose a single statement in brackets
 
func real FloatMul(A, B); \floating point version
real A, B; \arguments are declared here as floating point (doubles)
return A*B;</lang>
 
=={{header|XSLT}}==
Templates are the closest things XSLT has to user defined functions. They can be declared to be called by name and/or to be applied to all nodes in a matching set and given "mode". Both types of template can take named parameters with default values. Templates also have a "context" node used as the base of XPath expressions (kind of like an implied "this" of an object's method).
<lang xslt><xsl:template name="product">
<xsl:param name="a" select="2"/>
<xsl:param name="b" select="3"/>
<fo:block>product = <xsl:value-of select="$a * $b"/></fo:block>
</xsl:template>
 
<xsl:call-template name="product">
<xsl:with-param name="a">4</xsl:with-param>
<xsl:with-param name="b">5</xsl:with-param>
</xsl:call-template></lang>
 
<xsl:call-template name="product"/> &lt;-- using default parameters of 2 and 3 -->
 
=={{header|Yorick}}==
<lang yorick>func multiply(x, y) {
return x * y;
}</lang>
Example of interactive usage:
<pre>> multiply(2, 4.5)
9</pre>
 
=={{header|ZX Spectrum Basic}}==
On the ZX Spectrum, function names are limited to one letter. Note that the function becomes effective as soon as it is entered into the program, and does not need to be run
<lang zxbasic>10 PRINT FN m(3,4): REM call our function to produce a value of 12
20 STOP
9950 DEF FN m(a,b)=a*b</lang>
 
{{omit from|GUISS}}
Anonymous user