Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Arithmetic as variable?

Asked by
Mokiros 135
8 years ago

I have function in which perform arithmetic on standart numbers.

function arithmetic(formula)
    x = 1
    y = 10
    z = formula
    print(z)
end

arithmetic(x+y)

The problem is: it starts to count in function variables, when x and y are nil, and error happens because x and y are nil at the first line.

So i want to perform arithmetic only when i want to, in this case only on line 4.

1 answer

Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
8 years ago

x & y are not defined globally in the script in line 8. When you're calling a function, it takes in arguments for the function to play with.

function Arithmetic(x, y) -- Parameters for arguments
    local Solution = x + y
    print(Solution)
end
-- At this point, the function is defined; the output will spit out the solution when it is called.

Arithmetic(1, 10) -- Here, the defined function "Arithmetic" is called, taking 1 and 10 for x and y respectively.

11

Ad

Answer this question