193 posts
Posted 08 May 2012 - 03:40 AM
Well when I mean "error("Error. You needed to make the function Piggy()")"
I auctally mean error()
As a means to exit the program. Terminate it in its footsteps.
But no error message.
Why do I not hear about this?
Why did I have to discover it on my own?
Is there something bad about it or something?
1604 posts
Posted 08 May 2012 - 06:26 PM
Well, I don't think that's the best way to close your program, since you are saying to the caller that an error ocurred. It's better to use return outside of functions, or break your program's loop.
Examples:
local args = { ... }
if #args ~= 2 then -- check if you get the correct arguments
print("Usage: ...")
return -- exit the program
end
-- some more code
while true do -- infinite loop
-- some code here
if something then -- condition to exit the program
break -- break the loop and exit
end
end
local bExit = false
local function close()
bExit = true
end
while not bExit do
-- your program's code
end
193 posts
Posted 09 May 2012 - 11:13 AM
idk.
When I do this its not very helpful
print("First print!")
return
print("Shouldnt see this. But you will")
2447 posts
Posted 09 May 2012 - 11:24 AM
idk.
When I do this its not very helpful
print("First print!")
return
print("Shouldnt see this. But you will")
If you get the print afterwards, there is something you are doing wrong. Perhaps you are only exiting from a function?
193 posts
Posted 09 May 2012 - 02:30 PM
Im running that as a stand alone program
1111 posts
Location
Portland OR
Posted 09 May 2012 - 10:21 PM
Yeah you need to be in a function or loop for it to work. Try doing
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program…
Edited on 09 May 2012 - 08:24 PM
2447 posts
Posted 09 May 2012 - 10:26 PM
You're right - I'm sure this used to work from within the main body, but guess not.
The best way is to include all your code in a main() function - and then return from that when you want to end the program.
1548 posts
Location
That dark shadow under your bed...
Posted 15 June 2012 - 08:43 AM
That does work, just call the function as well. so:
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
returnTest()
351 posts
Posted 15 June 2012 - 06:01 PM
Yeah you need to be in a function or loop for it to work. Try doing
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program…
This is the problem with languages where whitespace is not significant(*cough*pythonisawesome*cough*). You have a newline there, but lua does not care. Lua just has specific rules it runs by. Any time you write "return" it takes everything up to the next "end" or the end of the file, and tries to return it. So, what you have there is equivalent to:
function returnTest()
print("First print!")
return print("The print will be run, and whatever it returns (nil) will be returned from the function")
end
To get the right behaviour, you have to put it in an artificial block, so it gets the end it expects.
function returnTest()
print("First print!")
do return end
print("You wont see this, because you are now as smart as the machine :(/>/>")
end
2447 posts
Posted 15 June 2012 - 06:53 PM
Yeah you need to be in a function or loop for it to work. Try doing
function returnTest()
print("First print!")
return
print("Shouldnt see this. But you will")
end
Hmm nevermind that doesn't work either. I thought I had used something like this before to exit a program…
This is the problem with languages where whitespace is not significant(*cough*pythonisawesome*cough*). You have a newline there, but lua does not care. Lua just has specific rules it runs by. Any time you write "return" it takes everything up to the next "end" or the end of the file, and tries to return it. So, what you have there is equivalent to:
function returnTest()
print("First print!")
return print("The print will be run, and whatever it returns (nil) will be returned from the function")
end
To get the right behaviour, you have to put it in an artificial block, so it gets the end it expects.
function returnTest()
print("First print!")
do return end
print("You wont see this, because you are now as smart as the machine :(/>/>")
end
In practice though, this doesn't cause an issue - you'd usually be returning from within a condition. Why would you put code after a return statement, if you expect the code to end at that point?
351 posts
Posted 15 June 2012 - 07:20 PM
I wasn't asking why, I was just trying to explain the example. The use of error for exiting seems like no problem to me, except in very strange cases, I guess.