# Passing Constants into Functions as local variables vs default parameters or global constants

**URL:** <https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177>\
**Category:** Performance\
**Tags:** question\
**Created:** [November 2, 2024, 1:46pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177 "2024-11-02T13:46:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hssn15](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@hssn15](https://discourse.julialang.org/u/hssn15)\
**Post date:** [November 2, 2024, 1:46pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/1 "2024-11-02T13:46:00Z")

</div>

I am writing Module that calculates thermodynamic properties of water. Module consists of lots of functions and each function requires 34 constant values. Fortunately, those parameters are same for all functions.

Currently, I m declaring those parameters as constant arrays inside the module.

My question is what is the best way of passing constant coefficients into the function in terms of faster computation.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2024, 1:57pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/2 "2024-11-02T13:57:44Z")

</div>

If they are truly module-wide constants that will never be changed by the user, e.g. Boltzmann’s constant, then I would just declare them as global constants in the module? e.g. using

```julia
const kB = 1.380649e-23 # Boltzmann's constant (J/K)

```

will be just as fast as pasting that literal value into your code.

> [@hssn15](#):
>
> Currently, I’m declaring those parameters as constant arrays inside the module.

It only makes sense to put them into an array if you are going to loop over them? If they are unrelated constants, e.g. Boltzmann’s constant, the density of water, and the specific heat of water, then an array makes less sense (and will be slower and more awkward to access).

On the other hand, if they are parameters that are potentially going to be changed by the user, it would make more sense to wrap them into a `struct MyParams` that you optionally pass into your functions, probably using [`Base.@kwdef`](https://docs.julialang.org/en/v1/base/base/#Base.@kwdef) so that the parameters have default values. e.g. you could declare your functions as `function foo(args....; params::MyParams=default_params)` where `default_params` is a global constant of default parameter values, so that you can omit the `params` argument in the common case where you are using the default values.

---

<div class="post-metadata">

**Author:** ![hssn15](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@hssn15](https://discourse.julialang.org/u/hssn15)\
**Post date:** [November 2, 2024, 3:05pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/3 "2024-11-02T15:05:06Z")

</div>

Thank you for your reply It was very helpful.

Yes, those parameters are truly constant throughout the module. I think, I will pass them as a default value.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2024, 3:06pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/4 "2024-11-02T15:06:31Z")

</div>

> [@hssn15](#):
>
> Yes, those parameters are truly constant throughout the module. I think, I will pass them as a default value.

I’m confused: Why pass them at all if they never change (are “truly constant”)? Why not just declare them as global constants?

---

<div class="post-metadata">

**Author:** ![hssn15](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@hssn15](https://discourse.julialang.org/u/hssn15)\
**Post date:** [November 2, 2024, 3:16pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/5 "2024-11-02T15:16:32Z")

</div>

I thought passing them as a default parameter would be faster in terms of computation. ( I could be wrong, I am new to julia). If there’s no difference, I’ll only use them as global constant.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 2, 2024, 3:24pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/6 "2024-11-02T15:24:55Z")

</div>

The advantage of having them as parameters, even if they are truly constant, is that it allows a greater variety of tests, and the possible flexibility of later supporting different units.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2024, 5:52pm UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/7 "2024-11-02T17:52:28Z")

</div>

> [@hssn15](#):
>
> I thought passing them as a default parameter would be faster in terms of computation. ( I could be wrong, I am new to julia). If there’s no difference, I’ll only use them as global constant.

No, if anything a global constant will be faster.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 3, 2024, 11:24am UTC](https://discourse.julialang.org/t/passing-constants-into-functions-as-local-variables-vs-default-parameters-or-global-constants/122177/8 "2024-11-03T11:24:33Z")

</div>

You may also be interested in this package to define the constant values:

> **[GitHub - JuliaPhysics/PhysicalConstants.jl: Collection of fundamental physical constants with...](https://github.com/JuliaPhysics/PhysicalConstants.jl)**
>
> Collection of fundamental physical constants with uncertainties. It supports arbitrary-precision constants
