Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2007, 6:55 PM   #11
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
Quote:
Originally Posted by DaWei View Post
Please read the forum's rules/faq. In the absence of code tags I can't tell if your code is really ugly, or just seems that way.
oops. fair enough:

1)
function eat($subject, $object, $verb, $adjective){
 if (! isset($verb)){$verb = "eats";}
 if (isset($adjective)){
  return $subject." ".$verb." ".$adjective." ".$object.".";
 }else{
  return $subject." ".$verb." ".$object.".";
 }
}

eat("man", "chicken");
will return "man eats chicken" and the verb and the adjective weren't nescessarily. It'll work but you will get warning in your error log that it was "expecting" four parameters and only got two.

on the other hand
eat("man", "chicken", "savors", "juicy", "pickle", "my", "bottom");
will return "man savors juicy chicken" and leave no warning message what so ever. Giving too many arguments does nothing at all.

2)
function eat($subject, $object, $verb="eats"){
 $adjective = func_get_arg(3);
 if ($adjective){
  return $subject." ".$verb." ".$adjective." ".$object.".";
 }else{
  return $subject." ".$verb." ".$object.".";
 }

}

eat("man", "chicken");
will return "man eats chicken." and gave no warnings. Why? function only expects three arguments but the $verb has a default setting to "eats" so that when it isn't supplied explicitly, "eats" is supplied by default.
eat("man", "chicken", "savors", "juicy", "pickle", "my", "bottom");
will return "man savors juicy chicken." Why? The function was "expecting" three parameters ($subject, $object, and $verb) and got them. The line:
$adjective = func_get_arg(3);
means that if there is a fourth parameter (3 is the index of the 4th parameter), set $adjective to it. If there isn't a fourth parameter $adjective is set to FALSE.

3)
function eat(){
 $args = func_get_args();
 $num = func_num_args();
 if ($num < 2){
  die ("Need to give two arguments at least");
 }
 else {
  $subject = $args[0];
  $object = $args[1];
 }
 if ($nums == 2){
  $verb = "eats";
 }else{
  $verb = $args[2];
 }
 if ($nums >= 4){
  $adjective = $args[3];
  return $subject." ".$verb." ".$adjective." ".$object.".";
 }else{
  return $subject." ".$verb." ".$object.".";
 }
}
eat("man", "chicken");
returns "man eats chicken." Why? eat() doesn't "expect" any parameters.

$args = func_get_args();
sets $args to the array ("man", "chicken").
$num = func_num_args();
set $num equal to 2. $num == 2 so $verb is set to "eats". $num is not >= 4 so "man eats chicken." is returned.
eat("man", "chicken", "savors", "juicy", "pickle", "my", "bottom");
yeilds "man savors juicy chicken". Which is pretty clear as $args ==("man", "chicken", "savors", "juicy", "pickle", "my", "bottom") and $num == 7.

---
As a rule of thumb define the parameters you need provided, set defaults for parameters you need but expect to be optional, and don't define the optional ones.

example:
function member($name, $id, $hobby){
  if (! isset($id)) $id = set_id();
  $hobbies = array_splice(func_get_args(),2);
}
Then
member("woozy",,"php","cooking", "skiing","being obtuse", "calling in sick");
will set $hobbies equal to the array ("php", "cooking", "skiing", etc.) and you can send as many arguments as you darn well please.
woozy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining languages titaniumdecoy Other Programming Languages 12 Jul 13th, 2006 2:03 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
libraries matko C 1 Jan 22nd, 2006 2:12 PM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC