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
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Fixed pos args
Var pos args
Fixed keyword args
Var keyword args
Mixed pos and keyword args
Call by value
Anon functions
Callbacks
Closures
Higher-order
Regex
Strings
Python Functions
Fixed positional args
No args def f():
  return "hello"
a=f()
hello
Single arg (int) def f(n):
  return n+1
n=f(1)
2
Single arg (string) def f(a):
  return ("good"+a)
a=f("bye")
goodbye
Multiple args (int) def f(a,b):
  return a+b
n=f(1, 2)
3
Multiple args (string) def f(a,b):
  return a+b
a=f("hello", " world")
hello world
Multiple args (wrong types) def f(a,b):
  return a*b
x="hello"
y="world"
try:
  a=f(x,y)
except:
  a="error"
error
Variable positional args
Count def f(*args):
  return len(args)
n=f(1,"hello",5.5)
3
First arg def f(*args):
  return args[0]
n=f(1,"hello",5.5)
1
Second arg def f(*args):
  return args[1]
a=f(1,"hello",5.5)
hello
Third arg def f(*args):
  return args[2]
d=f(1,"hello",5.5)
5.5
Fixed keyword args
Call in positional order def f(a, b):
  return a+" is "+b
a=f(a="less", b="more")
less is more
Call in non-positional order def f(a, b):
  return a+" is "+b
a=f(b="more", a="less")
less is more
Call without default arg def f(a="foo", b=""):
  return a+" is "+b
a=f(b="bar")
foo is bar
Call with args in structure def f(a, b):
  return a+" is "+b
args={
  'a' : "more",
  'b' : "less"
}
a=f(**args)
more is less
Variable keyword args
Count def f(**kargs):
  return len(kargs)
a=f(a="more", b="less")
2
Get keyword def f(**kargs):
  return kargs['b']
a=f(b="two", a="one")
two
Mixed positional and keyword args
Fixed positional and variable keyword (count keyword) def f(name, **kargs):
  return len(kargs)
a=f(name="alice", extra1="bob", extra2="carol")
2
Fixed positional and variable keyword (get keyword) def f(name, **kargs):
  return kargs['extra1']
a=f(name="alice", extra1="bob", extra2="carol")
bob
Variable positional and variable keyword (count positional) def f(*pargs, **kargs):
  return len(pargs)
a=f(name="alice", extra1="bob", extra2="carol")
0
Variable positional and variable keyword (count positional) def f(*pargs, **kargs):
  return len(pargs)
a=f("alice", extra1="bob", extra2="carol")
1
Variable positional and variable keyword (get positional) def f(*pargs, **kargs):
  return pargs[0]
a=f("alice", extra1="bob", extra2="carol")
alice
Call by value (aka pass by value)
Int reference def f(x):
  x=2
a=1
f(a)
1
String reference def f(x):
  x="new value"
a="original value"
f(a)
original value
List reference def f(x):
  x=[4, 5, 6]#this will cause a new list [4,5,6] to be assigned to x locally
a=[1, 2, 3]
f(a)
1 2 3
List reference - modify object referenced def f(x):
  x.append(4)
a=[1, 2, 3]
f(a)
1 2 3 4
List reference - overwrite object referenced def f(x):
  x[:]=[4,5,6]#this will overwrite the original list with new value
a=[1, 2, 3]
f(a)
4 5 6
Anonymous functions (aka lambda functions)
Create in place n/a inc_by_3_fp=lambda x: x+3
n=inc_by_3_fp(0)
3
Create by factory def mk_inc_f(n):
  return lambda x: x+n
inc_by_3_fp=mk_inc_f(3)
n=inc_by_3_fp(1)
4
Callbacks
Named def 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
def f(x):
  return (x + m)
n=f(1)
2
From enclosed function (g) read variable (m) defined in enclosing function (f) def f():
  m=2
  def g(x):
    return (x + m)
  a=g(1)
  return a
n=f()
3
Return function (inc_f) that reads variable (m) defined in enclosing function (mk_twice_inc_f) def mk_twice_inc_f(n):
  m=n*2
  def inc_f(x):
    return (x + m)
  return inc_f
incBy=2
f=mk_twice_inc_f(incBy)
incBy=666
n=f(0)
4
Higher-order
Take function as input def inc_by_1_f(x):
  return (x+1)
def 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 def inc_by_2_f(x):
  return (x+2)
def mk_twice_f(f):
  return lambda x : f(f(x))
f=mk_twice_f(inc_by_2_f);
n=f(1)
5