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
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.
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.
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