Link to home
Start Free TrialLog in
Avatar of asaidi
asaidi

asked on

calling function with more parameters

Hi
how i can call a function with more paras
ex:
if i have function A(a,b,c){
....
}
and i will call this function but i have 2 more parameters to pass
it is like i call it with A(a,b,c,d,e)
can you do this or is there another method..
any help please
ASKER CERTIFIED SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asaidi
asaidi

ASKER

and $d and $e are then have values in the function not like a,b,c ..
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> and $d and $e are then have values in the function not like a,b,c ..
I do not understand.

$a, $b, $c, $d and $e ALL have values in the function.  For $a, $b, $c - they are the passed values in the call of the function.
test_params(         'a','b','c','d',     5); // calls the function passing values as parms
// The passed parms   |   |   |   |       |
// are related by     |   |   |   |       |
// location in list:  V   V   V   V       V 
function test_params($a, $b, $c, $d="xyz",$e=123) { // receives the parms
	....

Open in new window


This is a rigid structure.  If you need flexibility ... as I mentioned and @julianH demonstrated - use arrays as the passed parameter.
Avatar of asaidi

ASKER

Hi
can i pass this
 p5($date,$date2,$report,$ser,$client=$client,$netw=$netw);
instead of d='xyz' and e=123
i put $client receives $client it is a varibale and the same for the netw
If the variable is defined in the functions definition ... i.e.
   function p5 ($v1, $v2, $v3, $v4, $v5)
yes ... $client will be $v4 in the function and $netw will be $v5.

If you called it:
   p5($date,$date2,$report,$ser,$client=$client,$netw=$netw, $another);
it will work OK ... but the extra parameter will be ignored by the function.
I think you are confused between a declaration

function p5($a, $b, $c, $d='123', $e='abc');

and calling the function

$value = 1;
p5('1', $value, 'blue');

With the above when the p5 function is entered

$a => '1'
$b => 1
$c => 'blue'
$d =>'123' // takes on the default value for the parameter because no parameter was passed
$e=>'abc'  // takes on the default value for the parameter because no parameter was

can you give more information on what you are trying to do - you are asking questions about parameters but if we know more about what you want to achieve we might be able to answer the question better.
Avatar of asaidi

ASKER

ok
i have a form that has
customer list it is a scrolling list
net unit :list scroll of water units
date1
date2
if the user leave blank customer report will print all the customers for a unit
my function will be p5($netw,$date1,$date2)
another scenario the user can choose a customer and a unit
my function will be
include('report.php');
p5($client,$netw,$date1,$date2)
i want to write one program that prints total and etc..not for each scenario i create a program
means p5,p6,p7..
i hope you understand me
i need one and can hold the params send by user
Then take a look at my earlier post html#a38295450

That is one of dealing with this alternatively declare your function as lwadwell demonstrated above
p5($client, $netw, $date1, $date2)

And pass null values or empty strings for the values you don't want the function to deal with.

Either way you are going to have to put some logic in the p5 function to test each of the parameters and output based on whether they have a value or not.
Firstly - to be very clear about this - the order you pass the variables matters.  Going from
  p5($netw,$date1,$date2)
to
  p5($client,$netw,$date1,$date2)
where the new parameter comes first will not work.  If you want optional parameters ... they should go last.  e.g.
  p5($netw,$date1,$date2,$client)

Hence the function could be setup to default the $client value, e.g.
  function p5($v1, $v2, $v3, $v4="ALL CLIENTS") { ...

Then in the function have logic that handles when $v4 == "ALL CLIENTS" differently to when a value is passed in the function call.

Or you could bundle them all into an array and pass the array.