This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Adjgamer's profile picture

Calling a Function From Another Function

Started by Adjgamer, 16 December 2012 - 05:45 AM
Adjgamer #1
Posted 16 December 2012 - 06:45 AM
So I am writing a little text rpg and I need to know how to call another function or restart the function I am in.
Thanks in advance.


function getName()
write("Name please? ")
name = read
print("Your name is ".. name"?")
write("y/n?")
check = read
if check == "y" then
print("Good, Let's move on!")
-- i want to move to the next function
else print("Oh? Try again!")
--restart the function
nolongerexistant #2
Posted 16 December 2012 - 07:11 AM

function getName()
write("Name please?: ")
uName = read() -- You forgot the () in "read()"

print("Is your name "..uName.."?") -- You need the '..' on both sides of the variable if you're printing text after it
write("y/n ")
uNameCheck = read() -- The () again
uNameCheck = uNameCheck:lower() -- Convert the input to lowercase, in case someone types Y or N instead of y/n

if uNameCheck == "y" then
  print("Good, let's move on then!")
  nameOfNextFunction() -- Just call the function like you did to this one
else
  print("Oh, let's try again then!")
  getName() -- Just call this function again
end
end

Aditionally, you can change

if uNameCheck == "y" then
  print("Good, let's move on then!")
  nameOfNextFunction() -- Just call the function like you did to this one
else
  print("Oh, let's try again then!")
  getName() -- Just call this function again
end

to

if uNameCheck == "y" then
  print("Good, let's move on then!")
  nameOfNextFunction() -- Just call the function like you did to this one
else
  print("Oh, let's try again then!")
  sleep(2) -- Wait 2 seconds to make let the user read the printed text
  term.clear() -- Clear the screen
  term.setCursorPos(1,1) -- To keep the screen neat.
  getName() -- Just call this function again
end
Just to keep the screen nice and clear if you restart it.


EDIT: Fixed a little issue.
Ulthean #3
Posted 16 December 2012 - 07:14 AM
Answer to your question:
Suppose you declared a function getName():

function getName()
-- some code
end

Then you call the function by just typing

getName()
Ulthean #4
Posted 16 December 2012 - 07:18 AM
Answer to a possible future question:
Suppose you declared a function getAverage(num1, num2):

function getAverage(num1, num2)
  return (num1+num2)/2
end

Then you call the function and get the result returned by the function by just typing

average=getAverage(2, 4)
Adjgamer #5
Posted 16 December 2012 - 08:28 AM
Thank you snackybo and Ulthean! Wow, I made a load of simple mistakes :o/> I don't know what your last post meant, Ulthean…
ChunLing #6
Posted 16 December 2012 - 09:57 AM
Use if read() == "y" then print("Good, Let's move on!") return name end

Also, make name local to the function. Then use a loop structure so you don't have to recurse the function. Thus:
function getName()
    local name
    repeat
        write("Please enter your name: ")
        name = read()
        print("Is your name "..name.."? y/n")
    until read() == "y"
    print("Good, Let's move on!")
return name end
Use this with an assignment to get the return. Like: local username = getName()

Argh, format correctly, code tags!
Edited on 16 December 2012 - 09:00 AM
Lyqyd #7
Posted 16 December 2012 - 11:05 AM
Do not use recursion as a way to create loops. Use a loop instead.
theoriginalbit #8
Posted 16 December 2012 - 01:35 PM
Do not use recursion as a way to create loops. Use a loop instead.

Agreed, only use recursion when appropriate. creates a call on the stack every time, and who knows how bit the CC stack is, you may eventually have it overflow and crash the app.