Logo

Programming-Idioms

  • PHP
  • Go
  • JS
declare(strict_types=1);
function square(int $x): int
{
    return $x * $x;
}

// square(4) -> 16
// square(3.4) -> Uncaught TypeError: Argument 1 passed to square() must be of the type int, float given
func square(x int) int {
  return x*x
}

The return type is after the parameter list
const square = n => n**2

The exponentiation operator is the most expressive way to do this.
const square = (x) => x * x;
function square(x) { 
	return x * x;
}
const square = (number) => Math.pow(number, 2);
function Square (X : Integer) return Integer is
begin
   return X * X;
end Square;

New implementation...
< >
programming-idioms.org