Recursive programming

Clear[g]

g[1] = 1

g[n_] := n*g[n - 1]

g[305]

$RecursionLimit = 500

g[305]

Clear[g]

g[n_] := If[n == 1, 1, n*g[n - 1]]

g[5]

Modules

f[x_, 
        y_] := 
(z = 
          x + y; 
z^2)

f[1, 1]

z

f[x_, 
        y_] := 
Module[{z}, 
z = x + y; 
        z^2]

z

f[2, 2]

z

Functions as arguments

Clear[f, g, h]

Function[x, x^2][2]

g = Function[x, x^2]

g

g[2]

h[f_, 
        x_] := 
(g = Function[x, f]; 
g[
          g[2]])

h[x^2, x]

g

Bisection example

mybisect[g_, x_, a0_, b0_, 
        tol_] := 
Module[{a, b, c, f, 
          i = 1}, 
a = N[a0]; 
        b = N[b0]; 
f = 
          Function[x, g]; 
If[
          f[a]*f[b] < 
            0, 
While[(b - a)/2 > 
              tol, 
c = (a + b)/
                2; 
If[f[a]*f[c] < 0, b = c, a = c]]; 
          c, 
Print["Function outputs have same sign at \
endpoints."]
  ]
]

mybisect[x^2 - 2, x, 0, 2, 10^-4]