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

Pass parameters to called function

Started by shournk, 13 May 2014 - 09:13 PM
shournk #1
Posted 13 May 2014 - 11:13 PM
When defining a function, how do I pass it's parameters to be parameters of a function it calls? I.e.


function monWrap(location)
  mon = peripheral.wrap("location")
end

and a person would then call monWrap(left). Or, for another example, if I want to pass program arguments to be parameters of a shell command,


local arg = { ... }
function reload(filename,code)
  fs.delete(filename)
  shell.run("pastebin get "code" "filename)
end

if #args ~= 2
  reload(arg[1],arg[2])
end

I feel like there's a lot wrong with that second one. How do I do this properly? I know this is elementary programming but I can't figure it out.
CometWolf #2
Posted 14 May 2014 - 12:24 AM
You pass all arguments the same way, by serperating them with commas. In the case of shell.run, you can either concatenate it to one string, or seperate each word with a comma. Your first code dosen't really make much sense, you appear to define location as an argument, then you pass the string "location" to the subsequent function calls? Just remove the quotes.