# Have slightly modified function compiled without copying code

**URL:** <https://discourse.julialang.org/t/have-slightly-modified-function-compiled-without-copying-code/102864>\
**Category:** General Usage\
**Created:** [August 16, 2023, 2:44pm UTC](https://discourse.julialang.org/t/have-slightly-modified-function-compiled-without-copying-code/102864 "2023-08-16T14:44:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [August 16, 2023, 2:44pm UTC](https://discourse.julialang.org/t/have-slightly-modified-function-compiled-without-copying-code/102864/1 "2023-08-16T14:44:37Z")

</div>

I keep running into cases where I have a function that has a base case, but sometimes I want to call it with an extra parameter so it behaves slightly differently. However, I don’t want to write two functions (lots of duplicate code), and I don’t want the runtime overhead of the adjustment when it’s not called with the extra parameter.

An oversimplified example:

```julia
# This works:
test(x, extra=0) = 2 * x + extra
# but I want the equivalent of:
test(x) = 2 * x
test(x, extra) = 2 * x + extra

```

I want two compiled functions: one that executes 2_x and another that executes 2_x + extra. Is there an existing mechanism to accomplish this? I can see how it could be done with a macro, but checking if it already exists or if there is some other/better way to accomplish it.

Note: even if the compiler is smart enough to compile away the +0 in this particular example, that’s just a contrived example. Other cases are more complex.

I’m aware of @generated, but it seems overkill for this case. Though maybe there is an easy way to accomplish it with @generated?

---

<div class="post-metadata">

**Author:** ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)\
**Post date:** [August 16, 2023, 2:51pm UTC](https://discourse.julialang.org/t/have-slightly-modified-function-compiled-without-copying-code/102864/2 "2023-08-16T14:51:14Z")

</div>

Perhaps not in a more complicated case but in your simplified example you can do this

```julia
test(x) = 2*x
test(x,extra) = test(x) + extra

```

Here that doesn’t save much but in a more complex example it could.

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [August 16, 2023, 3:04pm UTC](https://discourse.julialang.org/t/have-slightly-modified-function-compiled-without-copying-code/102864/3 "2023-08-16T15:04:48Z")

</div>

Right, thanks, but it’s the more complex case I’m concerned about. For example, a 10-20 line function with an operation in the middle. So, how to deal with cases where refactoring would not be an ideal solution.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 16, 2023, 3:11pm UTC](https://discourse.julialang.org/t/have-slightly-modified-function-compiled-without-copying-code/102864/4 "2023-08-16T15:11:45Z")

</div>

You could use something like this:

```julia
function test1(x, extra=nothing)
    a = 2*x
    b = if isnothing(extra)
        a
    else
        a + extra
    end
    c = sqrt(b)
    return c
end

```

The compiler will know whether `extra=nothing` (either through explicit assignment or the default value) so, not only will this work, but it will resolve the branch at compile time rather than run time.

Alternatively, the same concept using dispatch (which will also be resolved at compile time):

```julia
function text2(x, extra=nothing)
    a = 2*x
    b = applyextra(a, extra)
    c = sqrt(b)
    return c
end

applyextra(a, extra::Nothing) = a
applyextra(a, extra) = a + extra

```
