Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bothersome parameters 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hello ;)

Here is something I've always wanted to know.
How do you prevent functions from returning an error when you ommit parameters in the call?

function a_simple_function(param1, param2) {

}

print(a_simple_function(param1));

I'm asking this because, when I add functionalities to an old function, I don't want to have to add the additional parameters in the past calls that don't use the added features anyway.

Thanks !
 
Oh ... so, it's called "arguments" !

Thanks ;)

Then, I guess I just have to edit all my functions just like this :

function a_simple_function($arg1 = NULL, $arg2 = NULL) {

}

Right?
 
Correct.

If the argument is missing, PHP will supply the default.

The gotcha is when you have a function which has arguments with defaults and arguments without. The arguments without defaults must come first, else PHP will start providing the default values in the wrong places.



Want the best answers? Ask the best questions! TANSTAAFL!
 

Yeah, it's described in Example 17-10. but it got me wondering :

why not then make all the arguments NULL?
Why would one want a mix of default and non-default arguments?

I don't get it :(
 
It depends on reasonable assumptions.

Suppose you are writing a function for a computer system at a bicycle factory. 95% of all the pedal-powered vehicles produced by the factory are bicycles, but that other 5% are tricycles. The factory makes bicycles (and tricycles) in 5 colors, but there is no clear favorite color among customers.

A function which might place an item into an order at this factory might begin:

function add_item ($color, $number_of_wheels = 2)
{


The $number_of_wheels argument (or parameter, if you will. That's the word my college professors used.) has a default simply because it is a reasonsable assumption at this factory that when a bicycle is added to the order, it will nearly always have two wheels. $color does not have a default because there is no reasonable assumption that can be made about what the "default" color will be, as color choices are evenly distributed.

If the distribution of numbers of wheels on vehicles produced by this factory was 50% 2-wheels and 50% three-wheels, the function might not have had a default parameter at all.




Want the best answers? Ask the best questions! TANSTAAFL!
 

I see.
But to me, it doesn't look wise in term of portability/scalability.

My functions are all located in the same directory and manage several different websites.

This is the reason why I thought it would be wise to make every argument of every function NULL so that :
- I don't have to edit the past websites when a function is upgraded.
- Things such as the percentage asumption you were describing can be managed on function call only.

That's why I don't see any benefit to have default values for arguments. To me, it has benefits only when all the arguments are set to NULL for the reasons listed above.

Or maybe I'm misunderstanding something?

 
Default values aren't there for portability or scalability. They're there to reduce programmer keystrokes.

Take a look at the PHP manual entries for phpinfo() and print_r() for PHP-defined functions that have default parameters and see how they are used.

If you're changing a function so radically that you're changing the number of input parameters, you really should be writing a whole new function for your library. Leave the older function in there for older sites and have newer sites use the new version.

And if all your functions can have all their parameters set to NULL and still work, I wonder why you need those parameters at all.

Or maybe you should be looking at variable-length argument lists.



Want the best answers? Ask the best questions! TANSTAAFL!
 

...
And if all your functions can have all their parameters set to NULL and still work, I wonder why you need those parameters at all.
...

Now you're confusing me : aren't the "nullified" arguments supposed to receive a value on function call ??

If we have ...

function a_simple_function($arg1 = NULL, $arg2 = NULL) {
return $arg1;
}

print(a_simple_function("toto"));

... should output "toto", right?


 
aren't the "nullified" arguments supposed to receive a value on function call ??
Supposed to? I don't know: I don't know what you mean by that. PHP will not require that the parameters have explicitly-specified values at call time -- after all, those parameters already have default values of NULL available. Look at example 17-8 back on the page about default parameter values.


... should output "toto", right?
Yes. But we're talking about default parameters here. What is your function supposed to do when invoked as:

[tt]print(a_simple_function());[/tt]

Again, look at example 17-8



Want the best answers? Ask the best questions! TANSTAAFL!
 
...
Yes. But we're talking about default parameters here. What is your function supposed to do when invoked as:

print(a_simple_function());
...

To me, it sounds more logical to create functions such as :

Code:
function a_simple_function($arg1 = NULL, $arg2 = NULL) {

if($arg1 != NULL) // do something that uses $arg1

if($arg2 != NULL) // do something that uses $arg2

if($arg1 == NULL AND $arg2 == NULL) // do a default action that needs no argument at all.

}
 
I'd sure hate to have to maintain code written by someone who used default values for all the arguments. I'd wonder why they were all optional, and how the function got so convoluted to necessitate such ambiguity.

Lee
 

It seems obvious that I'm not a real programmer ;)

Still, I'm curious to know : how would your functions look like if you wanted to make every of them scalable with all your sites, past and future ??

Why couldn't the concept found in classes be applied to functions as well?

I mean, classes are supposed to be highly portable and reusable via the use of its parameters, right?

What if one wanted to use the same logic/strategy with functions?



 
As I have said before, if I were to so radically change a function that I had to change the number of input parameters it uses, then I would leave the old version in place and create a new version. Both versions would then reside in my library until such time as I could go back and fix my older code calls.

But it is a very rare thing, indeed, that I have to change the definition of a function. I may change how it works internally, but it's interface to calling code remains the same. I don't just create functions willy-nilly -- when I'm developing a library that I know I will use for a long time, I take a great deal of time to think about what the library will do and how it will do it. I do my best to plan in advance for future needs before the library gets "set in stone" by its being called from production code. And typically, functions in my code libraries are sufficiently special-purpose that their definition is fairly obvious.

Yes, all this applies to classes and their methods, too.



Want the best answers? Ask the best questions! TANSTAAFL!
 

So, the main difference between you and me is that you prefer to create multiple special-purpose functions when I prefer to lower the number of functions and make them almost multi-purpose.

Still, I think you can't really foresee every future need for a a function.

Let's take a practical example : I have a function that manage the swap between languages on multilingual sites.
Before, it was designed to output little flags that directed to the current content but in a different language.
Recently, I've added layout options such as vertical/horizontal alignment of the flags, replacement of flags by text, css styles, etc... The "flags area" is output by one single function that needs parameters to display the proper layout.

Now, don't you think it's cool when you just copy your newly upgraded function on an old site and that this old site still continues to run smoothly?

Of course, you could leave the old functions as it is but, then, you would have to keep track of which version is older and which one is totally outdated.

Well, I'm not trying to lecture you but I'm simply explaining the reasoning behind the "weird" way I do things :)


 
Of course, you could leave the old functions as it is but, then, you would have to keep track of which version is older and which one is totally outdated.
To date, I haven't had much problem with this. Generally, when I am one of the architects of the code library, I am going to work the design of the function library to death, play-testing it and making as sure as I can it's okay. That way, gross changes to the library are minimal, and are usually only adding functions to add new functionality (pun intended).

But it's not a matter of how many inputs a function has. It's a matter of looking in sufficient depth at the question the function is intended to answer. Changes to the number of inputs is something to be avoided -- if not now, certainly should you find yourself developing software as part of a team.

I have functions that can, depenind on circumstances, take a different number of inputs. But those functions generally always only take one input parameter: an associative array with the varying number of inputs in it.



Want the best answers? Ask the best questions! TANSTAAFL!
 
There's a difference between ambiguity and flexibility. Why not just have one catch-all master function that handles doing everything? That, of course, would be a HUGE, bloated function that would be so encumbered with conditionals and code that it wouldn't be practical or very useful.

I write functions to create modularity. If I have something (code, HTML, whatever) that is repeated, I try to put it in a separate function so I can reuse it without reinventing the code over and over. In programming classes you learn that it's good programming practice to use functions to do one thing and one thing only.

sleida said:
It seems obvious that I'm not a real programmer ;)

I certainly wouldn't display or wear that as a badge of honor. There's a reason why experienced programmers do things certain ways, and why newbies/novices don't. There's a reason why "new" concepts developed by those who are inexperienced are viewed as old mistakes by experienced programmers. Some of use have to learn from our own mistakes, but it's MUCH easier and quicker to learn from the mistakes and experience of others who have done this before us. There are still plenty of errors to make even relying on what others have done.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top