Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Fixed pos args
Var pos args
Var keyword args
Call by value
Anon functions
Callbacks
Closures
Higher-order
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
JavaScript Functions
Fixed positional args
No args function f()
{
  return "hello";
}
a=f()
hello
Single arg (int) function f(n)
{
  return n+1;
}
n=f(1)
2
Single arg (string) function f(a)
{
  return "good"+a;
}
a=f("bye")
goodbye
Multiple args (int) function f(a,b)
{
  return a+b;
}
n=f(1,2)
3
Multiple args (string) function f(a,b)
{
  return a+b;
}
a=f("hello ","world")
hello world
Multiple args (wrong types) function f(a,b)
{
  r=a*b;
  if (r!=r)
  {
    r="error";
  }
  return r;
}
a=f("hello","world")
error
Variable positional args
Count function f()
{
  return arguments.length;
}
n=f(1,"hello",5.5);
3
First arg function f()
{
  return arguments[0];
}
a=f(1,"hello",5.5);
1
Second arg function f()
{
  return arguments[1];
}
a=f(1,"hello",5.5);
hello
Third arg function f()
{
  return arguments[2];
}
a=f(1,"hello",5.5);
5.5
Variable keyword args
Count function f(context)
{
  return Object.keys(context).length;
}
a=f({a:'more', b:'less'});
2
Get keyword function f(context)
{
  return context['b'];
}
a=f({b:'two', a:'one'});
two
Call by value (aka pass by value)
Int reference function f(x)
{
  x=2;
}
a=1;
f(a);
1
String reference function f(x)
{
  x="new value";
}
a="original value";
f(a);
original value
List reference function f(x)
{
  x=[4, 5, 6];
}
a=[1, 2, 3];
f(a);
1 2 3
List reference - modify object referenced function f(x)
{
  x.push(4);
}
a=[1, 2, 3];
f(a);
1 2 3 4
List reference - overwrite object referenced function f(x)
{
  x.length=0
  Array.prototype.push.apply(x, [4, 5, 6]);
}
a=[1,2,3];
f(a);
4 5 6
Anonymous functions (aka lambda functions)
Create in place n/a inc_by_3_fp=
  function(x) { return x+3; }
n=inc_by_3_fp(0);
3
Create by factory function mk_inc_f(n)
{
  return function (x)
  {
    return x+n;
  };
}
inc_by_3_fp=mk_inc_f(3);
n=inc_by_3_fp(1);
4
Callbacks
Named function cb_function(a)
{
  return a*a;
}
cb_handle=cb_function;
n=cb_handle(3);
9
Closures
From enclosed function (f) read variable (m) defined in enclosing block {
  m=1;
  function f(x)
  {
    return (x + m);
  }
}
n=f(1)
2
From enclosed function (g) read variable (m) defined in enclosing function (f) function f()
{
  m=2;
  function g(x)
  {
    return (x + m);
  }
  return g(1)
}
n=f();
3
Return function (inc_f) that reads variable (m) defined in enclosing function (mk_twice_inc_f) function mk_twice_inc_f(n)
{
  m=n*2;
  function inc_f(x)
  {
    return (x + m);
  }
  return inc_f;
}
incBy=2;
f=mk_twice_inc_f(incBy);
incBy=666;
n=f(0);
4
From enclosed function (f) write variable (m) defined in enclosing block {
  m=1;
  function f()
  {
    m += 4;
  }
}
f();
5
From enclosed function (g) write variable (m) defined in enclosing function (f) function f()
{
  m=1;
  function g()
  {
    m += 5;
  }
  g();
  return m;
}
n=f();
6
Higher-order
Take function as input function inc_by_1_f(x)
{
  return (x+1);
}
function twice_f(f, x)
{
  return f(f(x));
}
n=twice_f(inc_by_1_f,1);
3
Take function as input and output a function function inc_by_2_f(x)
{
  return (x+2);
}
function mk_twice_f(f)
{
  return function(x)
  {
    return f(f(x));
  }
}
f=mk_twice_f(inc_by_2_f);
n=f(1);
5