# Define a Julia function

**URL:** <https://discourse.julialang.org/t/define-a-julia-function/76054>\
**Category:** New to Julia\
**Tags:** question, functions\
**Created:** [February 9, 2022, 1:16am UTC](https://discourse.julialang.org/t/define-a-julia-function/76054 "2022-02-09T01:16:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)\
**Post date:** [February 9, 2022, 1:16am UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/1 "2022-02-09T01:16:31Z")

</div>

My question is about which way is better to define this function:  
Way 1:

> function α(x, _a, b, α\_0, Lmin, Lnom, Lmax_)  
> if Lmin \<= x \< Lnom  
> return a \* (x-Lnom)^2 + α\_0  
> elseif Lnom \<= x \<= Lmax  
> return b \* (x-Lnom)^2 + α\_0  
> end  
> end  
> Way 2:  
> function α(x)  
> _a, b, α\_0, Lmin, Lnom, Lmax_ = **Calculations**  
> if Lmin \<= x \< Lnom  
> return a \* (x-Lnom)^2 + α\_0  
> elseif Lnom \<= x \<= Lmax  
> return b \* (x-Lnom)^2 + α\_0  
> end  
> end

As shown above, I want to define a function α(x). The other  
parameters are constants. These constants are **calculated** outside of α(x).

1. I want to ask for some suggestions on whether I should put these constants as  
arguments or directly put them within α(x)?
2. I think it would be more convenient to put them inside of the function (together with related calculations). My concern is, I need to use this function to do repeated optimization. Will this **slow my program** (since these parameters need to be calculated)?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 9, 2022, 1:52am UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/2 "2022-02-09T01:52:13Z")

</div>

if you gonna repeatedly call `α(x)` and Calculations does calculation, then you don’t want to repeat calculation inside `α(x)`

---

<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:** [February 9, 2022, 2:55am UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/3 "2022-02-09T02:55:47Z")

</div>

> [@Jian\_ZUO](#):
>
> _a, b, α\_0, Lmin, Lnom, Lmax_ = **Calculations**

Depends on what “Calculations” are. The compiler can constant-fold some thing.

---

<div class="post-metadata">

**Author:** ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)\
**Post date:** [February 9, 2022, 8:43am UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/4 "2022-02-09T08:43:47Z")

</div>

Thanks. In my case, it’s just some simple calculations like curve fitting from known points and some arithmatic calculations.  
And there are some parameters in the calculation that I need to modify for different study cases, that’s why I don’t want to manually assign specific numbers in the function definition.

---

<div class="post-metadata">

**Author:** ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)\
**Post date:** [February 9, 2022, 8:44am UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/5 "2022-02-09T08:44:58Z")

</div>

Thanks. Yes, that’s the case. I need to repeatedly call this function in my program.

---

<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:** [February 9, 2022, 12:13pm UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/6 "2022-02-09T12:13:36Z")

</div>

> [@Jian\_ZUO](#):
>
> And there are some parameters in the calculation that I need to modify for different study cases, that’s why I don’t want to manually assign specific numbers in the function definition.

In that case, I would define a data structure (`struct`) to hold the calculation results. Then write two functions: one (probably a [constructor](https://docs.julialang.org/en/v1/manual/constructors/)) to initialize the data structure instance `d` from the parameters, and another function function `α(x, d)`. Or potentially you could make `d` a [callable object](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects) (which often makes sense for a curve-fitting result), in which case you could write `d(x)`.

---

<div class="post-metadata">

**Author:** ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)\
**Post date:** [February 9, 2022, 12:54pm UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/7 "2022-02-09T12:54:40Z")

</div>

Thanks for the suggestions. I will try them in my program.  
I just thought about simply adding the **global** keyword inside the function to call  
these parameters.  
Will this slow my program?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 9, 2022, 1:35pm UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/8 "2022-02-09T13:35:22Z")

</div>

If the global variables you are accessing are not constants (i.e., do not have `const` before their name in their definition), then most surely your code will be slowed.

---

<div class="post-metadata">

**Author:** ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)\
**Post date:** [February 9, 2022, 1:38pm UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/9 "2022-02-09T13:38:10Z")

</div>

I see. Thanks for the reply.

---

<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:** [February 9, 2022, 2:00pm UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/10 "2022-02-09T14:00:48Z")

</div>

It’s not just performance. Non-constant global variables in _any_ programming language are an obstacle to software engineering in the long run. Google “why global variables bad” for lots of explanations.

---

<div class="post-metadata">

**Author:** ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)\
**Post date:** [February 9, 2022, 3:44pm UTC](https://discourse.julialang.org/t/define-a-julia-function/76054/11 "2022-02-09T15:44:34Z")

</div>

Thanks!
