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
9 years ago

I have function in which perform arithmetic on standart numbers.

1function arithmetic(formula)
2    x = 1
3    y = 10
4    z = formula
5    print(z)
6end
7 
8arithmetic(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
9 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.

1function Arithmetic(x, y) -- Parameters for arguments
2    local Solution = x + y
3    print(Solution)
4end
5-- At this point, the function is defined; the output will spit out the solution when it is called.
6 
7Arithmetic(1, 10) -- Here, the defined function "Arithmetic" is called, taking 1 and 10 for x and y respectively.

11

Ad

Answer this question