crazyman10123
crazyman10123

Reputation: 58

Lua - How do I use a function from another script?

I've been looking around and I have not been able to find anything that has worked for me. I'm starting to learn more Lua and to start off I'm making a simple calculator. I was able to get each individual operation onto separate programs, but when I try to combine them I just can't get it to work. My script as it is now is

require "io"
require "operations.lua"

do
print ("Please enter the first number in your problem.")
x = io.read()
print ("Please enter the second number in your problem.")
y = io.read()
print ("Please choose the operation you wish to perform.")
print ("Use 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division.")
op = io.read()
op = 1 then
    function addition
op = 2 then
    function subtraction
op = 3 then
    function multiplication
op = 4 then
    function division
print (answer)
io.read()
end

and my operations.lua script is

function addition
    return answer = x+y
end

function subtraction
    return answer = x-y
end

function multiplication
    return answer = x*y
end

function division
    return answer = x/y
end

I've tried using

if op = 1 then
      answer = x+y
      print(answer)
if op = 2 then
      answer = x-y
      print(answer)

and I did that completing each operation. But it doesn't work. I can't even get the error code that it's returning because it closes so fast. What should I do?

Upvotes: 3

Views: 642

Answers (3)

Corbin March
Corbin March

Reputation: 25714

In your example, make these changes: You require operations.lua without the extension. Include parameters in your operations function definitions. Return the operation expression directly versus returning a statement like answer = x+y.

All together:

Code for operations.lua

function addition(x,y)
    return x + y
end

--more functions go here...

function division(x,y)
    return x / y
end

Code for your hosting Lua script:

require "operations"

result = addition(5,7)
print(result)

result = division(9,3)
print(result)

Once you get that working, try re-adding your io logic.

Keep in mind that as it's coded, your functions will be defined globally. To avoid polluting the global table, consider defining operations.lua as a module. Take a look at the lua-users.org Modules Tutorial.

Upvotes: 1

uzsolt
uzsolt

Reputation: 6027

The right if-then-else syntax:

if op==1 then
   answer = a+b
elseif op==2 then
   answer = a*b
end
print(answer)

After: please check the correct function-declaration syntax.

After: return answer=x+y is incorrect. If you want set answer's value, set without return. If you want return the sum, please use return x+y.

And I think you should check Programming in Lua.

Upvotes: 1

filmor
filmor

Reputation: 32222

First of all, learn to use the command line so you can see the errors (on Windows that would be cmd.exe).

Second, change the second line to require("operations"). The way you did it the interpreter expects a directory operations with an underlying script lua.lua.

Upvotes: 0

Related Questions