You want an anonymous function:
Simple solution:
function create_f(flag::Bool)
if flag
x -> x^2
else
x -> x^3
end
end
The above solution is, however, not type stable:
julia> using Test
julia> @inferred create_f(true)
ERROR: return type var"#1#3" does not match inferred return type Union{var"#1#3", var"#2#4"}
A type stable solution:
function create_f(flag::Bool)
m = flag ? 2 : 3
let n = m
x -> x^n
end
end
Now the type is uniquely inferred:
julia> using Test
julia> @inferred create_f(true)
#1 (generic function with 1 method)