Lua: Writing simple method using return statement

4 Jul 2016
Karthik Padmanabhan
140
0

Today, we will learn and use methods and return statement in our Program. Programming in Lua is more like Programming in C. If you are familiar with C then Lua is pretty easy to learn.

Program code:

function IsEven (n)
    if n%2 == 0 then
        return "Even"
    else
        return "Odd"
    end
end

print("Enter a Number: ")
a=io.read("*number")
print("Number ",a,"is ",IsEven(a))

In the above program, we have created a method IsEven(...), which takes a number as input and identifies if it is Even or Odd and returns a string which is printed as output to the console. Also, if you notice, we have used a=io.read(...), this allows us to take an input from the user and read it using system io stream and assigns it to the variable a for us to make use of.

The next program that we will learn will contain more code and will be a little more extensive. Stay tuned for that.

Leave a Reply

Your email address will not be published. Required fields are marked *