Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

defaulting reference parameters in user function
I have created a user function that is passed a number of parameters and
a few of these must be passed by reference.

However, two of the parameters that are passed by reference are optional
- or at least I'd like them to be.

However, if I do this:

function foo($firstParm,$secondParm,&$thirdParm=false,&$lastParm=false)
{
if ($thirdParm)
.
if ($lastParm)
.

..
}

and call it like this:

foo("first","second");

I get "parse error, unexpected '=', expecting ')'

It seems that I cannot default variables that are defined as passed by
reference... or is there some provision for doing so that is a trick of
the trade?

I realize that it would work to pass in an array by reference to do the
same thing, but it would mean re-writing a lot of code.


Report this thread to moderator Post Follow-up to this message
Old Post
Mark Richards
04-15-05 08:56 AM


Re: defaulting reference parameters in user function
"Mark Richards" <nospam@massmicro.com> wrote in message
news:425f1fd5$0$5889$9a6e19ea@news.newshosting.com...
>I have created a user function that is passed a number of parameters and a
>few of these must be passed by reference.
>
> However, two of the parameters that are passed by reference are optional -
> or at least I'd like them to be.
>
> However, if I do this:
>
>  function foo($firstParm,$secondParm,&$thirdParm=false,&$lastParm=false)
>  {
>   if ($thirdParm)
>     ..
>   if ($lastParm)
>     ..
>
>   ...
>  }
>
> and call it like this:
>
>   foo("first","second");
>
> I get "parse error, unexpected '=', expecting ')'
>
> It seems that I cannot default variables that are defined as passed by
> reference... or is there some provision for doing so that is a trick of
> the trade?
>
> I realize that it would work to pass in an array by reference to do the
> same thing, but it would mean re-writing a lot of code.

This is a user-comment posted at php.net:

-----

Functions explicitly prototyped with formal parameters passed by reference
can't have default values. However, functions prototyped to assign default
values to formal parameters may be passed references.

For example, this is a parse error:

function foo(&$bar = null) {
// formal parameters as references can't have default values
$bar = 242;
}

While this is perfectly legal (and probably what you want, mostly):

function foo($bar = null) {
$bar = 242;
}

foo();    // valid call, no warnings about missing args
foo(&$x); // valid call, post $x == 242

-----
posted by  bishop 03-Jun-2003 12:06
http://fi2.php.net/manual/en/functions.arguments.php

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com



Report this thread to moderator Post Follow-up to this message
Old Post
Kimmo Laine
04-15-05 08:56 AM


Re: defaulting reference parameters in user function
Mark Richards wrote:
> I have created a user function that is passed a number of parameters and
> a few of these must be passed by reference.
>
> However, two of the parameters that are passed by reference are optional
> - or at least I'd like them to be.
>
> However, if I do this:
>
>  function foo($firstParm,$secondParm,&$thirdParm=false,&$lastParm=false)
...
>
> It seems that I cannot default variables that are defined as passed by
> reference... or is there some provision for doing so that is a trick of
> the trade?
>

It's worth noting that there's no benefit in passing a simple data-type
(e.g. booleans) by reference unless you want to alter the original
variable. But in supplying default values, it's clear that you don't
want to alter any existing variables. Therefore you should pass these
parameters by value, not by reference.

Passing by reference should only be used in situations where you want
the function to alter its value, or where there would be a lot of
overhead in creating a copy to send, e.g. a long string or a big array
or object.


--
Oli

Report this thread to moderator Post Follow-up to this message
Old Post
Oli Filth
04-15-05 08:56 PM


Re: defaulting reference parameters in user function
Ollie and Kimmo,

Thank you for the suggestions.

The variables I was passing by reference are arrays.  I posted an
inaccurate example.

I didn't realize that in php you can specify pass by reference "&" at
the function call.  This actually makes a lot more sense (to me) and
follows C syntax.

Here's what I did:

Function definition:

function  mpd($aDataX,$aDataY,$aDataYMidnight=0,$a
DataYNoon=0)
{
if (is_array($aDataYMidnight))
do something...
}

And here's the call:

mpd($aDataX,$aDataY,&$aDataYMidnight,&$aDataYNoon);

Or this one:

mpd($aDataX,$aDataY);

Both work nicely.

Thanks!

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Richards
04-16-05 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:20 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.