Confused.. Explaining This Output?

I’m just coding around experimenting with how functions work and am stumped of the outcome of this code. The outcome to the console is … string string works. Now I don’t understand why string is printed twice. If i make a == a real number it prints the number twice also… If i remove the function ret() then it only prints once. Whats going on?

[lua]a = “stringS”
b = 5
function calc()
if (type(a) == “number”) then
print (a + 10)
elseif (type(a) ~= “number”) then
print (type(a))
return type(a)
end
end
calc()

function ret()
if (calc() == “string”) then
print (“Works!”)
end
end
ret() [import]uid: 20272 topic_id: 34803 reply_id: 334803[/import]

calc() is executed twice. Once on row 11, and once again on row 14 when you call ret() [import]uid: 70847 topic_id: 34803 reply_id: 138331[/import]

Ok if I take cacl() out it works. But if i remove ret() from line 18 neither function runs. From what I read I thought I had to have both because a function isn’t executed unless called. How come I still have to call ret() ? [import]uid: 20272 topic_id: 34803 reply_id: 138337[/import]

You are correct. A function isn’t executed unless called.
In your case lines 3-10 define function calc(), and lines 13-17 define function ret().
If you don’t call calc() or ret() nothing is executed.

When you call ret(), calc() is also executed since you’re calling it on line 14 within ret(). [import]uid: 70847 topic_id: 34803 reply_id: 138345[/import]

Got ya, thanks. [import]uid: 20272 topic_id: 34803 reply_id: 138358[/import]

calc() is executed twice. Once on row 11, and once again on row 14 when you call ret() [import]uid: 70847 topic_id: 34803 reply_id: 138331[/import]

Ok if I take cacl() out it works. But if i remove ret() from line 18 neither function runs. From what I read I thought I had to have both because a function isn’t executed unless called. How come I still have to call ret() ? [import]uid: 20272 topic_id: 34803 reply_id: 138337[/import]

You are correct. A function isn’t executed unless called.
In your case lines 3-10 define function calc(), and lines 13-17 define function ret().
If you don’t call calc() or ret() nothing is executed.

When you call ret(), calc() is also executed since you’re calling it on line 14 within ret(). [import]uid: 70847 topic_id: 34803 reply_id: 138345[/import]

Got ya, thanks. [import]uid: 20272 topic_id: 34803 reply_id: 138358[/import]