# How to define an auxiliary function within a function [duplicate]

I would like to define a function within a function to serve as an auxiliary function. In essence, something like this:

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


Where both f and g are in reality complicated functions. But this does not work because Module, or With, do not accept rule delayed in their definitions.

f[1,2]
Module::lvset: Local variable specification {g[z_]:=2 z} contains g[z_]:=2 z, which is an assignment to g[z_]; only assignments to symbols are allowed.


Is there a way around it?

• Check the second example in documentation for Module... – Kuba Jun 8 '17 at 9:43
• Indeed, I missed it. – Whelp Jun 8 '17 at 9:47

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