Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- C
- Caml
- Clojure
- C++
- C#
- C#
- C#
- C#
- D
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Groovy
- Haskell
- Haskell
- Haskell
- JS
- JS
- JS
- JS
- Java
- Java
- Java
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Ruby
- Rust
- Scala
- Scheme
- Scheme
- Smalltalk
- VB
- VB
function Square (X : Integer) return Integer is
begin
return X * X;
end Square;
int square(int x){
return x*x;
}
let square x = x*x
(defn square [x]
(* x x))
int square(int x){
return x*x;
}
int Square(int x)
{
return x * x;
}
int square(int x) {
return x*x;
}
int square(int x) => x * x;
@spec square(integer) :: integer
def square(x) when is_integer(x), do: x*x
The guard guarantees that the return value is an integer. The spec lets dialyzer warn you if you use this without something guaranteed to be an integer, or where the return value is used somewhere that an integer is invalid.
-spec square(integer()) -> integer().
square(X) when is_integer(X) -> X * X.
The guard guarantees that the return value will be an integer.
module foo
implicit none
contains
function square(i) result(res)
integer, intent(in) :: i
integer :: res
res = i * i
end function square
end module foo
Modern Fortran code should use modules, so I put the function into one.
func square(x int) int {
return x*x
}
The return type is after the parameter list
int square(int x){
return x*x
}
square x = x * x
square x = x**2
Result is a Floating
square x = x^2
Result is an Integral
const square = (x) => x * x;
const square = (number) => Math.pow(number, 2);
const square = n => n**2
The exponentiation operator is the most expressive way to do this.
Function<Integer,Integer> squareFunction = x -> x * x;
int square(int a) { return a * a; }
interface F { int get(int a); }
F square = x -> x * x;
int square(int x){
return x*x;
}
fun square(x: Int) = x * x
(defun square (x)
(* x x))
function square(x)
return x*x
end
int square(int x) {
return x*x;
}
Precisely same as plain C
Function square(x: Integer): Integer;
Begin
Result := x*x;
End;
sub square {
my ($i) = @_;
return $i ** 2;
}
Explicit use of 'return' is optional, but good practice.
def square(x):
return x**2
You can use power operator
square = lambda x: x * x
def square(x):
return x*x
You don't have to explicitly write the return type
def square(x) = x*x
Ruby 3.0
def square(x)
x*x
end
The last expression is returned by default.
fn square(x : u32) -> u32 { x * x }
Last expression in block is used as a return value.
def square(x:Int): Int = x*x
(define square
(lambda (x)
(* x x)))
(define (square x)
(* x x))
This is a short syntax for a lambda definition.
[:anInteger | anInteger squared]