# Preprocessing function arguments

**URL:** <https://discourse.julialang.org/t/preprocessing-function-arguments/57474>\
**Category:** New to Julia\
**Created:** [March 18, 2021, 1:30pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474 "2021-03-18T13:30:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![huchiayu0517](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huchiayu0517/32/8202_2.png) [@huchiayu0517](https://discourse.julialang.org/u/huchiayu0517)\
**Post date:** [March 18, 2021, 1:30pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474/1 "2021-03-18T13:30:29Z")

</div>

I have a working code like the following:

```julia
function test(a,b)  
        c = a + b
        d = a - b
        e = c + 2 * d 
end

```

but I now want to sometimes make `b` a constant s.t.

```julia
const b = 1.5
function test(a)
        c = a + b
        d = a - b
        e = c + 2 * d 
end

```

My experience with C makes me want to do something like

```julia
#ifdef USECONST
const b = 1.5
function test(a)
#else
function test(a,b)
#endif
        c = a + b
        d = a - b
        e = c + 2 * d 
end

```

Is there a Julia syntax that can do this? I’m basically looking for an easy way for me to reuse the function body (c=…, d=…, e=…) while still have the option of using b as either a constant or an input parameter in different applications. Thanks!

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 18, 2021, 1:32pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474/2 "2021-03-18T13:32:04Z")

</div>

```julia
function test(a,b = 1.5)  
        c = a + b
        d = a - b
        e = c + 2 * d 
end

```

I believe that is what you want. With this you can call `test(3.3)` or call `test(3.3, 1.25)`. If `b` isn’t provided then it’s value will be 1.5.

---

<div class="post-metadata">

**Author:** ![huchiayu0517](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huchiayu0517/32/8202_2.png) [@huchiayu0517](https://discourse.julialang.org/u/huchiayu0517)\
**Post date:** [March 18, 2021, 1:39pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474/3 "2021-03-18T13:39:01Z")

</div>

Wow thank you for your instant answer! Unfortunately, in my case, b is loaded from a file and is in fact a large array that I cannot hard-code it…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 18, 2021, 2:01pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474/5 "2021-03-18T14:01:02Z")

</div>

> [@huchiayu0517](#):
>
> an easy way for me to reuse the function body (c=…, d=…, e=…) while still have the option of using b as either a constant or an input parameter in different applications.

> [@huchiayu0517](#):
>
> b is loaded from a file and is in fact a large array that I cannot hard-code it…

Then I don’t see the advantage of just defining `test(a, b)`, and calling it with a `b` you either define in the source or load from a file.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 18, 2021, 2:01pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474/6 "2021-03-18T14:01:04Z")

</div>

Maybe a closure?

```julia
function maketest(defaultB)
      return a -> begin
           c = a + defaultB
           d = a - defaultB
           e = c + 2*d
   end
end
test = maketest(1.5)

```

---

<div class="post-metadata">

**Author:** ![huchiayu0517](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huchiayu0517/32/8202_2.png) [@huchiayu0517](https://discourse.julialang.org/u/huchiayu0517)\
**Post date:** [March 18, 2021, 2:19pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474/7 "2021-03-18T14:19:30Z")

</div>

I realize I’m not giving the full picture. I’m trying to embed my Julia code from C calling the function `test(a,b)`. However, b is a struct containing several large arrays, and I don’t know how to do embedding with that. Fortunately, b is unchanged through the entire calculation of `test` so my solution is to write b into a file, or files as I have three different sets of b’s as options and load it back when start-up (when I do `using MyModule`). The original function test(a,b) is still useful when I’m in pure Julia mode and want to switch between different sets of b’s or explore a new b.

---

<div class="post-metadata">

**Author:** ![huchiayu0517](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huchiayu0517/32/8202_2.png) [@huchiayu0517](https://discourse.julialang.org/u/huchiayu0517)\
**Post date:** [March 18, 2021, 3:14pm UTC](https://discourse.julialang.org/t/preprocessing-function-arguments/57474/8 "2021-03-18T15:14:09Z")

</div>

I just realized that the default value of b doesn’t have to be hard-coded! The following code fits my need

```julia
struct Par{T}
    x::T
    y::T
    z::T
end

b_global = Par{Float64}(1,1,1)

function test(a, b=b_global)
        @show b
        #some code
end

test(1)
test(1,Par{Float64}(0,0,0))

```

Thanks everyone!
