Function definition: Difference between revisions
Content added Content deleted
(fix markup) |
|||
Line 3: | Line 3: | ||
Write a definition of a function called "multiply" that takes two arguments and returns their product. |
Write a definition of a function called "multiply" that takes two arguments and returns their product. |
||
=={{header|Ada}}== |
|||
[[Category:Ada]] |
|||
function multiply(a : float; b : float) return float is |
|||
begin |
|||
return a * b; |
|||
end multiply; |
|||
=={{header|C}}== |
=={{header|C}}== |
Revision as of 04:07, 19 September 2007
Function definition
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
A function is a body of code that returns a value. The value returned may depend on arguments provided to the function.
Write a definition of a function called "multiply" that takes two arguments and returns their product.
Ada
function multiply(a : float; b : float) return float is begin return a * b; end multiply;
C
double multiply( double a, double b ) { return a * b; }
MAXScript
fn multiply a b = ( a * b )
Perl
sub multiply( $$ ) { $a = shift; $b = shift; return $a * $b; }
PHP
function multiply( $a, $b ) { return $a * $b; }
Python
Named function:
def multiply(a, b): return a * b
Unnamed function:
multiply = lambda a, b: a * b
A callable - may be useful to allow both simple functions and complex classes to use the same interface:
>>> class Multiply: ... def __call__(self, a, b): ... return a * b ... >>> multiply = Multiply() >>> multiply(3, 4) 12
Toka
[ ( ab-c ) * ] is multiply