# Help me: function in Julia

**URL:** <https://discourse.julialang.org/t/help-me-function-in-julia/87523>\
**Category:** General Usage\
**Tags:** question, function\
**Created:** [September 20, 2022, 2:51pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523 "2022-09-20T14:51:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tryjulia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tryjulia/32/42826_2.png) [@tryjulia](https://discourse.julialang.org/u/tryjulia)\
**Post date:** [September 20, 2022, 2:51pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/1 "2022-09-20T14:51:12Z")

</div>

Hi, so I actually want to write a code that would show me the result of factorial of:  
x = x + 2  
y = x\*(x-1) \* y  
but when I tried to put it in function, it doesn’t behave the way I want to. It needs to define the x and y outside the function, but when I did it like this:

> x=1  
> y=0
> 
> function fact(x,y)
> 
> ```
> fact(n::Int) = n == 0 ? 1 : n*fact(n-1)
> while x!=n
> x = x + 2
> y = x*(x-1) * y
> end
> 
> ```
> 
> end  
> @show x y

the result of x and y are 1 and 0. The function doesn’t want to calculate the formula given.  
Where did I do wrong? Thank you

---

<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:** [September 20, 2022, 2:55pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/2 "2022-09-20T14:55:47Z")

</div>

You are trying to manipulate global variables, which is generally bad, and extra bad for performance in Julia. If that’s really what you want to do, then you need to mark them as global within your function.

[https://docs.julialang.org/en/v1/manual/variables-and-scoping/](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)  
[https://docs.julialang.org/en/v1/manual/performance-tips/](https://docs.julialang.org/en/v1/manual/performance-tips/)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 20, 2022, 2:57pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/3 "2022-09-20T14:57:31Z")

</div>

I don’t understand what you’re trying to do, but one thing that’s clear is that you didn’t call your function, but expect it to execute. It won’t unless you call it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 20, 2022, 3:56pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/4 "2022-09-20T15:56:43Z")

</div>

You also have this

> [@tryjulia](#):
>
> `fact(n::Int) = n == 0 ? 1 : n*fact(n-1)`

_inside_ your other `fact` definition, which just sits there doing nothing. What is it for?

---

<div class="post-metadata">

**Author:** ![tryjulia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tryjulia/32/42826_2.png) [@tryjulia](https://discourse.julialang.org/u/tryjulia)\
**Post date:** [September 20, 2022, 3:59pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/5 "2022-09-20T15:59:34Z")

</div>

so I’m trying to do this equation (in a function):

> increase x while maintaining invariant:  
> x=x+2  
> y=x\*(x-1)\*y

now I try to call the function by using this:

> x=0  
> y=1  
> n=6  
> function fact(x,y)
> 
> ```
> while x!=n
> p = x + 2
> q = x*(x-1) * y
> end
> 
> ```
> 
> end
> 
> (p,q)=fact(x,y)  
> @show x y

but I have been execute this for more than 5 minutes but no result come out yet. I’m using VS code anyway.

 ![debug](https://global.discourse-cdn.com/julialang/original/3X/f/8/f8761c5a5cf84a95e9e2dd280d82eefa13ff1132.png)

Thank you

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 20, 2022, 4:19pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/6 "2022-09-20T16:19:49Z")

</div>

There are many things that need improvement in your function, but the first thing is: The input value of `x` is 0, and `n` is 6. And since your function never updates `x`, it will never equal `n` and keeps looping forever.

Also, your function doesn’t return anything, so writing

> [@tryjulia](#):
>
> (p,q)=fact(x,y)

won’t work.

---

<div class="post-metadata">

**Author:** ![tryjulia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tryjulia/32/42826_2.png) [@tryjulia](https://discourse.julialang.org/u/tryjulia)\
**Post date:** [September 20, 2022, 4:34pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/7 "2022-09-20T16:34:52Z")

</div>

thank you. now it works!

> x=0  
> y=1  
> n=6  
> function fact(x,y)
> 
> ```
> while x!=n
> x = x + 2
> y = x*(x-1) * y
> end
> return x,y
> 
> ```
> 
> end
> 
> (x,y)=fact(x,y)  
> @show x y

---

<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:** [September 20, 2022, 4:50pm UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/8 "2022-09-20T16:50:58Z")

</div>

Your function still access global variable `n`. It would be better to make it a parameter, too.

---

<div class="post-metadata">

**Author:** ![tryjulia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tryjulia/32/42826_2.png) [@tryjulia](https://discourse.julialang.org/u/tryjulia)\
**Post date:** [September 21, 2022, 2:57am UTC](https://discourse.julialang.org/t/help-me-function-in-julia/87523/9 "2022-09-21T02:57:14Z")

</div>

thank you! changed it to:

> function fact(n)  
> x=0  
> y=1  
> while x!=n  
> x = x + 2  
> y = x\*(x-1) \* y  
> end  
> x,y  
> end  
> (x,y)=fact(6)  
> @show x y
