# Can a function return another function?

**URL:** <https://discourse.julialang.org/t/can-a-function-return-another-function/77806>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 12, 2022, 7:39pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806 "2022-03-12T19:39:43Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 12, 2022, 7:39pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/1 "2022-03-12T19:39:43Z")

</div>

Hello everyone! Can a function return another function?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 12, 2022, 7:40pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/2 "2022-03-12T19:40:28Z")

</div>

```julia
julia> foo(x) = y -> y*x
foo (generic function with 1 method)

julia> f = foo(4)
#4 (generic function with 1 method)

julia> f(3)
12

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 12, 2022, 7:43pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/3 "2022-03-12T19:43:04Z")

</div>

Yes, of course:

```julia
julia> function sincos(x)
          sin(x)*cos(x)
       end
sincos (generic function with 1 method)

julia> sincos(1)
0.4546487134128409

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 12, 2022, 7:54pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/4 "2022-03-12T19:54:35Z")

</div>

Looks to me like that is just a regular function which returns a numeric value.

But functions which return functions are no problem at all. Here is an example from `Base`:

```julia
julia> f = startswith("fun")
(::Base.Fix2{typeof(startswith), String}) (generic function with 1 method)

julia> f("function")
true

julia> f isa Function
true

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 12, 2022, 8:47pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/5 "2022-03-12T20:47:09Z")

</div>

> [@marllos](#):
>
> Hello everyone! Can a function return another function?

As others have already answered: yes. As I understand it: coming from a LISP/Scheme tradition of languages you can do quite a bit more with Julia. So what are you looking for, exactly?

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 12, 2022, 9:46pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/6 "2022-03-12T21:46:41Z")

</div>

Reading all the answers helped a lot. I don’t know if I’m doing it the best way. Simplifying the code a bit, now it works:

```julia
using QuadGK

function func1(f:: Function, T)
	integral = quadgk(f, 0, T)[1]
	normalized(x) = f(x)/integral # normalize
	normalized
end

function test()
	g(x) = x^2 + x + 1
	gn = func1(g, 2.0)
	println( gn(2.0) )
end

test()

```

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 13, 2022, 7:25pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/7 "2022-03-13T19:25:56Z")

</div>

To be more specific, I need to create a new function, inside a function and return this created function to be used in the main scope. I am not sure if I am doing this safely.

```julia
function create_new_func(f1::Function, T)
# new function to return: f1n(t)
	if T < 10
		f1n(t) = f1(t) + 1
	else
		f1n(t) = f1(t) + 2
	end
	f1n
end

function test(x)
	f(x) = 2x
	g = create_new_func(f, x)
	println(g(2)) # 5
end

test(1) # WARNING: Method definition 
# test(11) # ERROR: LoadError: UndefVarError: f1n not defined 

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 13, 2022, 7:52pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/8 "2022-03-13T19:52:12Z")

</div>

For this you need anonymous functions

> [@marllos](#):
>
> ```julia
> if T < 10
> f1n(t) = f1(t) + 1
> else
> f1n(t) = f1(t) + 2
> end
> 
> ```

So, do

```julia
if T < 10
	f1n = t -> f1(t) + 1
else
	f1n = t -> f1(t) + 2
end 

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 13, 2022, 8:10pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/9 "2022-03-13T20:10:56Z")

</div>

To be clear, this is a bug:  
[https://github.com/JuliaLang/julia/issues/15602](https://github.com/JuliaLang/julia/issues/15602)  
Using anonymous function is a work around it.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 13, 2022, 8:12pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/10 "2022-03-13T20:12:16Z")

</div>

Elrod answered similarly, but I hadn’t understood. The function I posted was a simple example of the structure I needed. Now it worked in my code. Thanks again DNF, thanks everyone.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 13, 2022, 8:19pm UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/11 "2022-03-13T20:19:21Z")

</div>

Wow, this sounds like a new entry in the `wat`-thread, especially considering the date this bug was opened… (I always assumed these forms to be synonymous)

Edit: although I assume there are some pretty difficult technicalities to solve this…

Edit: Ah, I remember, this is a difference between `f = x -> x` and `const f = x -> x`…

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 14, 2022, 12:18am UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/12 "2022-03-14T00:18:30Z")

</div>

That issue with conditional multimethods is one example of how Julia is not _as_ dynamic as other dynamic languages (another example is how we can’t simply redefine a struct). If the method table of a function was allowed to be conditional, it prevents a lot of optimization. On the other hand, if you fix the method table but allow variation of captured variables, you pretty much reuse the same underlying function that takes those captured variables as arguments, like a functor. So the compiler assumes that right now and builds the method table from every nested method, disregarding the conditions.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [March 14, 2022, 6:36am UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/13 "2022-03-14T06:36:12Z")

</div>

This just returns a numerical value, not a function. Maybe you meant

```julia
function sincos()
    x -> sin(x)*cos(x)
end
sincos (generic function with 1 method)

julia> sincos()(1)
0.4546487134128409

```

?

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [March 14, 2022, 9:19am UTC](https://discourse.julialang.org/t/can-a-function-return-another-function/77806/14 "2022-03-14T09:19:50Z")

</div>

```julia
function sin_cos(ch::Bool)
  if ch
    return sin
  else
    return cos
  end
end

```
