Jump to content




Calling function from another function in an API


3 replies to this topic

#1 TheUltimateKrtekCZ

  • New Members
  • 2 posts

Posted 29 October 2017 - 12:52 PM

Hello, guys.

I have a little problem with calling a function inside a function from the same API.

system/APIs/peripheralAPI:
function getPeripheralSideByType(peripheralType)
  return getPeripheralSideByType(peripheralType, count)
end

function getPeripheralSideByType(peripheralType, count)
  local peripheralFound = 0
  for i = 1, 6, 1 do
    if peripheralType == peripheral.getType(rs.getSides()[i]) then
     peripheralFound = peripheralFound + 1
     if peripheralFound == count then
      return rs.getSides()[i]
     end
    end
  end
  return nil
end

function getPeripheralByType(peripheralType)
  return getPeripheralByType(peripheralType, 1)
end

function getPeripheralByType(peripheralType, count)
  if getPeripheralSideByType(peripheralType, count) == nil then return nil
    else return peripheral.wrap(getPeripheralSideByType(peripheralType, count))
  end
end

test:
peripheralAPI.getPeripheralByType("monitor", 1)
-- returns table
peripheralAPI.getPeripheralByType("monitor")
-- returns nil

Pls Help!

#2 supernicejohn

  • Members
  • 98 posts
  • LocationSweden

Posted 29 October 2017 - 08:02 PM

Basically, function overloading isn't a feature in CC, however there are a couple implementations, http://www.computerc...ding-functions/ being the first to turn up in a google search.
If you don't want to use an api, you could have these checks inside the main functions themselves as there is nothing wrong with passing nil as function arguments (with incorrect # of arguments resulting in nil).
What happens when you run that file is that a global function getPeripheralByType gets created, which accepts 1 argument, then after that, it is overwritten by a function of the same name that accepts 2 arguments. The same is true for getPeripheralSideByType , after which you attempt to call the 2-argument function with one argument. Nothing wrong really, but in this case it's not what you want.

For example, in the getPeripheralSideByType function, you could place "count = count or 1" as a first line, and it will use '1' whenever a 'count' isn't specified.
Hope that helps!

#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 29 October 2017 - 08:02 PM

I'm not sure how that result could occur, assuming everything you've said is correct. Totally missed that you were overloading a function. Sorry!

That said, there is a function which can replicate most of the functionality you're implementing.

Edited by KingofGamesYami, 29 October 2017 - 08:03 PM.


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 October 2017 - 03:44 AM

 supernicejohn, on 29 October 2017 - 08:02 PM, said:

Basically, function overloading isn't a feature in CC

More specifically, it's not a feature of Lua.

When you define a function in the manner done in the OP, what you're doing is assigning a function pointer to a variable - much the same as when you assign a number or a string or whatever. If you consecutively assign two different functions to the one variable, then the first gets lost and only the second remains.

If you want two different functions, then you're going to have to assign them to two different variables. However, to expand on what supernicejohn was saying, Lua also doesn't care how many arguments you pass to a function: it's up to the function's code to handle or ignore however many arguments you want. For eg, you can do:

function example(arg)
   if arg then
      print("An argument was passed.")
   else
      print("No argument was passed.")
   end
end

example("Hello")   --# Prints "An argument was passed."
example()          --# Prints "No argument was passed."

Then there's the awesomeness that is the vararg function:

function example(...)
   print("I received " .. arg.n .. " arguments.")
end

example()        --# "I received 0 arguments."
example(1,2,3)   --# "I received 3 arguments."
example("4", 5)  --# "I received 2 arguments."






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users