# How to add condition for compiling functions?

**URL:** <https://discourse.julialang.org/t/how-to-add-condition-for-compiling-functions/88839>\
**Category:** New to Julia\
**Tags:** question, compilation, function\
**Created:** [October 17, 2022, 9:20am UTC](https://discourse.julialang.org/t/how-to-add-condition-for-compiling-functions/88839 "2022-10-17T09:20:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ludovic\_Dumoulin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludovic_dumoulin/32/22414_2.png) [@Ludovic\_Dumoulin](https://discourse.julialang.org/u/Ludovic_Dumoulin)\
**Post date:** [October 17, 2022, 9:20am UTC](https://discourse.julialang.org/t/how-to-add-condition-for-compiling-functions/88839/1 "2022-10-17T09:20:48Z")

</div>

Hey,

I have a code with multiple functions and variables in order to simulate a physical system.  
I would like to be able to use the same code and just with a `if(one input parameter)` to choose which numerical method I use for a sub-part of my system.  
The Issue is that I don’t want to declare all the arrays necessary for the different methods in my main while I will use only a part of them. But I if don’t declare all the array but use an `if` for the declaration, I have an error `UndefVarError` during compilation… Even if the value of the If will never change during the simulation …  
Exemple with a simple solution (set array=nothing on condition):

```julia
v_temp = fft ? nothing : CUDA.zeros(Float64, Nx, Nz, 2)
fP = fft ? CUDA.zeros(ComplexF64, Lkx, Lkz, 2) : nothing

if fft
   solve_FFT!(..., fP, ...)
else
   solve_iter!(..., v_temp, ...)
end

```

I don’t know if there is a better way of doing it ? (Without creating 2 different files because 80% of the rest is the same.)

Thank you in advance for your help, I am sure it should exist an easy way 🙂

---

<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:** [October 17, 2022, 9:30am UTC](https://discourse.julialang.org/t/how-to-add-condition-for-compiling-functions/88839/2 "2022-10-17T09:30:39Z")

</div>

There are probably better organization options at a large scale, but without knowing the whole picture a local change could be

```julia
if fft
    fP = CUDA.zeros(ComplexF64, Lkx, Lkz, 2)
    solver_specific_args = [fP]
else
    v_temp = CUDA.zeros(Float64, Nx, Nz, 2)
    solver_specific_args = [v_temp]
end

if fft
   solve_FFT!(..., solver_specific_args..., ...)
else
   solve_iter!(..., solver_specific_args..., ...)
end

```
