Jump to content


Photo

Number of arguements in a function


  • Please log in to reply
11 replies to this topic

#1 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 08 September 2006 - 02:49 PM

Hello,

I was just wondering if anyone here could tell me if there is a limit on the number of arguements that can be passed to a user defined function or if there are any disadvantages in having large numbers of arguments?

Thanks


Thanks Cep

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 08 September 2006 - 02:54 PM

Not that I'm aware of. You can always pass in array references or use func_num_args. If you have too many arguments, you should ask yourself what you're really try to accomplish because this is a design issue. You may need to break your function into multiple--each being a modular piece of the puzzle. You may also prefer the OOP route.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 °°Ben³

°°Ben³
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationGermany / Remagen

Posted 08 September 2006 - 02:56 PM

if there is a limit on the number of arguements that can be passed to a user defined function

I don't think so respectively I think if there is a limit it would be too high to reach. ;)

if there are any disadvantages in having large numbers of arguments?

Im my opinion you should not put too many arguments into a function, but there is no fix "rule" or something like that. I think that it becomes kind of chaotic, if you have eight or twelve parameters. If I'm honest, I cannot imagine why one should want to do this .. ;)

Regards, Ben.
desktop and web development ;)
---
English is not my native language. Please excuse if I made some errors in expression.

#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 08 September 2006 - 03:04 PM

The disadvantage to having large numbers of arguments is that you'll get them the wrong way round sometimes, or find it difficult to see which values you're passing as which argument.

If your function needs a large number of arguments consider creating a class instead and making it a member function of a class from which you can instantiate an object with the arguments as member variables.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 03:06 PM

the answer to how many arguments a function can have, the universe, and everything, is 42. 
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 08 September 2006 - 03:40 PM

Hehe thanks,

The reason for so many arguements is because I am creating several INSERT and UPDATE queries which I am going to be reusing all over the place, rather then write the code each time I thought I would place it in a function and then call that function when I need it.
Thanks Cep

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 September 2006 - 03:42 PM

what about building the query string and then passing just the one string to the function?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 04:17 PM

He might be using it for use in a query builder.. such as this method:
<?php

public function parseQuery()
{
    $num = func_num_args();
    if ($num == 1)
    {
        $this->_query = func_get_arg(0);
    }
    elseif ($num > 1)
    {
        $query = func_get_arg(0);
        for ($i = 1; $i < $num; $i++)
        {
            $arg = $arg = mysql_real_escape_string(func_get_arg($i), $this->_link);
            if (!is_numeric($arg))
            {
                $arg = "'" . mysql_real_escape_string($arg, $this->_link) . "'";
            }
            $query = str_replace(':' . $i, $arg, $query);
        }
        $this->_query = $query;
    }
    else
    {
        throw new InvalidArgumentException('Must provide a query to be parsed!');
    }
}

?>

usage..

<?php

$db->parseQuery('SELECT * FROM `table` WHERE `col` = :1 AND `col2` = :2', $colVal, $col2Val);

?>


#9 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 08 September 2006 - 04:27 PM

Also see mysqli_prepare.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#10 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 04:35 PM

indeed, but that could involve reworking 90+% of his site, and a headache full of troubles when some hosts don't use mysqli :)

#11 °°Ben³

°°Ben³
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationGermany / Remagen

Posted 08 September 2006 - 07:13 PM

Depending on what you want to do in the end, you may use an associative array as argument.
Though Jenk's suggestion is also a nice one .. ;)
desktop and web development ;)
---
English is not my native language. Please excuse if I made some errors in expression.

#12 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 11 September 2006 - 09:19 AM

Or if I'm not using Mysql :D but I didn't specify a database so that doesnt matter. Thankyou all for your suggestions, I think I may adapt Jenks idea or use an array depending on which is easier to implement at this stage.

Thanks!
Thanks Cep




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users