Function definition: Difference between revisions

From Rosetta Code
Content added Content deleted
(LSE64)
(Added BASIC example.)
Line 9: Line 9:
return a * b;
return a * b;
end multiply;
end multiply;

=={{header|BASIC}}==
'''Compiler:''' [[QuickBasic]] 4.5
DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
multiply = a * b
END FUNCTION



=={{header|C}}==
=={{header|C}}==

Revision as of 07:01, 20 November 2007

Task
Function definition
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;

BASIC

Compiler: QuickBasic 4.5

DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)

FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
multiply = a * b
END FUNCTION


C

double multiply( double a, double b )
{
  return a * b;
}

Macros

Macros can be defined at the top of a program and the compiler will replace the function calls with the function itself before compiling the program (the source file will not change). This is useful for less complex function as they won't take up any extra memory.

#define MULTIPLY(X, Y) ((X) * (Y))

Parentheses should be added around parameters in the function definition to avoid order of operations errors when someone uses the macro as such:

x = MULTIPLY(x + z, y);

A program with that call would be compiled as if this were coded instead:

x = ((x + z) * (y));

Common Lisp

(defun multiply(a b)
 (* a b))
(multiply '2 3)

Forth

: fmultiply ( F: a b -- F: c )  F* ;
: multiply ( a b -- c )  * ;

Haskell

 multiply = (*)

Alternatively,

 multiply = \ x y -> x*y

Java

There are no global functions in Java. The equivalent is to define static methods in a class (here invoked as "Math.multiply(a,b)"). Overloading allows us to define the method for multiple types.

public class Math
{
    public static    int multiply(   int a,    int b) { return a*b; }
    public static double multiply(double a, double b) { return a*b; }
}

JavaScript

function multiply(a,b) { return a*b }

LSE64

multiply  : *
multiply. : *.  # floating point

MAXScript

fn multiply a b =
(
    a * b
)

Pascal

function multiply(a,b: real): real;
begin
 multiply := a*b;
end;

Perl

sub multiply( $$ )
{
  $a = shift;
  $b = shift;
  return $a * $b;
}

PHP

function multiply( $a, $b )
{
   return $a * $b;
}

Pop11

define multiply(a, b);
    a * b
enddefine;

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

Raven

define multiply use a, b
    a b *

Or optional infix:

define multiply use a, b
    (a * b)

Or skip named vars:

define multiply *

Ruby

 def multiply(a, b)
   a * b
 end

Scheme

(define (multiply a b)
  (* a b))

Seed7

const func float: multiply (in float: a, in float: b) is
  return a * b;

Tcl

Strictly as described in the task:

proc multiply { arg1 arg2 } {
   return [expr $arg1 * $arg2]
}

But I'm not sure anybody would write it that way. If nothing else, there's no reason to prefer scalar arguments. And it is unclear why one would impose a limit on the number of arguments. I would write it like this:

proc multiply { args } {
   return [expr [join [join [join $args]] *]]
}

This will accept one or more arguments which can be scalars or lists (or even nested lists) and returns the product of all of them. E.g.

% multiply "1 2" {3 4} [list 5 6] {{7 8} 9}
 
=> 362880

Toka

[ ( ab-c ) * ] is multiply