473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Variable Argument Function Calls

Hey,

Just a quick appeal for suggestions re some code, able to pass varying
numbers of arguments...

I have a function which can accept varying numbers of arguments. It in
turn calls a function whose argument list can vary in length.

At the moment, I have something along the lines of...

************************************************** **********
function varargsfunction(){

switch(func_num_args()){
case 0:
$value = function($inputargs[0]);
break;

case 1:
$value = function($inputargs[0], $inputargs[1]);
break;

case 2:
$value = function($inputargs[0], $inputargs[1], $inputargs[2]);
break;

etc.......

}

return otherfunction($value);

}
************************************************** **********

It works fine, but I am obviously restricted by the length of my case
statement. Also, as you can see it's not a very pretty solution!!

Is there a way to dynamically build the argument list such that it does
not require me to write out every possible case into a big switch
statement?
Any help greatly appreciated!!
Thanks in advance.
Adam.

Aug 15 '06 #1
6 1509
Rik
Adam wrote:
Hey,

Just a quick appeal for suggestions re some code, able to pass varying
numbers of arguments...

I have a function which can accept varying numbers of arguments. It
in turn calls a function whose argument list can vary in length.

At the moment, I have something along the lines of...

************************************************** **********
function varargsfunction(){

switch(func_num_args()){
case 0:
$value = function($inputargs[0]);
break;
Euhm, this should break immedietaly.
From func_num_args() = 1 on, you can use

case 1:
$value = function($inputargs[0], $inputargs[1]);
break;

case 2:
$value = function($inputargs[0], $inputargs[1], $inputargs[2]);
break;

etc.......

}

return otherfunction($value);

}
************************************************** **********

It works fine, but I am obviously restricted by the length of my case
statement. Also, as you can see it's not a very pretty solution!!

Is there a way to dynamically build the argument list such that it
does not require me to write out every possible case into a big switch
statement?

<?php
function test($var1,$var2,$var3){
echo "$var1 $var2 $var3\n";
}

function varargsfunction(){
$array = func_get_args();
return call_user_func_array('test',$array);
}
varargsfunction('test1'); //issues a warning: missing parameters
varargsfunction('test1','test2','test3'); //this works
varargsfunction('test1','test2','test3', 'test4'); //this works, argument 4
is ignored
?>

Grtz,
--
Rik Wasmus
Aug 15 '06 #2
Adam wrote:
Hey,

Just a quick appeal for suggestions re some code, able to pass varying
numbers of arguments...

I have a function which can accept varying numbers of arguments. It in
turn calls a function whose argument list can vary in length.

At the moment, I have something along the lines of...

************************************************** **********
function varargsfunction(){

switch(func_num_args()){
case 0:
$value = function($inputargs[0]);
break;

case 1:
$value = function($inputargs[0], $inputargs[1]);
break;

case 2:
$value = function($inputargs[0], $inputargs[1], $inputargs[2]);
break;

etc.......

}

return otherfunction($value);

}
************************************************** **********

It works fine, but I am obviously restricted by the length of my case
statement. Also, as you can see it's not a very pretty solution!!

Is there a way to dynamically build the argument list such that it does
not require me to write out every possible case into a big switch
statement?
Any help greatly appreciated!!
Thanks in advance.
Adam.
Why not let the functions take an array? That way you can have as many
or few parameters as you wish.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 15 '06 #3
Rik
Jerry Stuckle wrote:
Adam wrote:
>Hey,

Just a quick appeal for suggestions re some code, able to pass
varying numbers of arguments...

I have a function which can accept varying numbers of arguments. It
in turn calls a function whose argument list can vary in length.
<snip code>
>It works fine, but I am obviously restricted by the length of my case
statement. Also, as you can see it's not a very pretty solution!!

Is there a way to dynamically build the argument list such that it
does not require me to write out every possible case into a big
switch statement?

Why not let the functions take an array? That way you can have as
many or few parameters as you wish.
While offcourse a better idea, one might want to adress an already existing
PHP function, where the behaviour cannot be altered easily. But in that
case, a simple call_user_func_array() in the code itself should suffice,
instead of making it a different function.

We might have better insight if the OP could explain what the function
should actually do.

Grtz,
--
Rik Wasmus
Aug 15 '06 #4
Thanks for your help so far... I had to type it from memory, so agree
that the "case 0:" was dead wrong!
The reason for using the multi arg input is twofold...

1. I am placing the vars into sprintf (to construct a MySQL query)
which is itself a variable arg function (hence why I don't think I can
use an array here).

2. Using an array just means I need a few extra lines of code to move
all of the random query bits into an intermediate array to stick into
the function. I would prefer to be able to list them in the function
call.

I am aware that there are other ways to do this, but I am aiming for an
ultra elegant solution. Maybe I'm being a bit pedantic about it!?! : )
Cheers again...
Adam

Rik wrote:
Jerry Stuckle wrote:
Adam wrote:
Hey,

Just a quick appeal for suggestions re some code, able to pass
varying numbers of arguments...

I have a function which can accept varying numbers of arguments. It
in turn calls a function whose argument list can vary in length.

<snip code>
It works fine, but I am obviously restricted by the length of my case
statement. Also, as you can see it's not a very pretty solution!!

Is there a way to dynamically build the argument list such that it
does not require me to write out every possible case into a big
switch statement?
Why not let the functions take an array? That way you can have as
many or few parameters as you wish.

While offcourse a better idea, one might want to adress an already existing
PHP function, where the behaviour cannot be altered easily. But in that
case, a simple call_user_func_array() in the code itself should suffice,
instead of making it a different function.

We might have better insight if the OP could explain what the function
should actually do.

Grtz,
--
Rik Wasmus
Aug 15 '06 #5
Rik
Adam wrote:
Thanks for your help so far... I had to type it from memory, so agree
that the "case 0:" was dead wrong!

The reason for using the multi arg input is twofold...

1. I am placing the vars into sprintf (to construct a MySQL query)
which is itself a variable arg function (hence why I don't think I can
use an array here).
Euhm. vsprintf()? Takes an array, loves it actually.

2. Using an array just means I need a few extra lines of code to move
all of the random query bits into an intermediate array to stick into
the function. I would prefer to be able to list them in the function
call.

I am aware that there are other ways to do this, but I am aiming for
an ultra elegant solution. Maybe I'm being a bit pedantic about
it!?! : )

Well, the best I can do to achieve what I think you want:
Call an arbitrary function with an arbitrary amount of arguments, switching
scalars and arrays like it doesn't care, the first argument given is the
function, be it in a scalar or the first scalar in the first array (of the
first array and so forth):

<?php

function flatten_array($arg){
if(!is_array($arg)) return array($arg);
$return = array();
foreach($arg as $row){
if(is_array($row));
$return = array_merge($return,flatten_array($row));
} else {
$return[] = $row;
}
}
return $return;
}

function varargsfunction(){
$array = func_get_args();
$array = flatten_array($array);
$function = array_shift($array);
return call_user_func_array($function,$array);
}
?>

Grtz,
--
Rik Wasmus
Aug 15 '06 #6
Thanks Rik, that's exactly what I was after!

Cheers,
Adam.
Rik wrote:
Adam wrote:
Thanks for your help so far... I had to type it from memory, so agree
that the "case 0:" was dead wrong!

The reason for using the multi arg input is twofold...

1. I am placing the vars into sprintf (to construct a MySQL query)
which is itself a variable arg function (hence why I don't think I can
use an array here).

Euhm. vsprintf()? Takes an array, loves it actually.

2. Using an array just means I need a few extra lines of code to move
all of the random query bits into an intermediate array to stick into
the function. I would prefer to be able to list them in the function
call.

I am aware that there are other ways to do this, but I am aiming for
an ultra elegant solution. Maybe I'm being a bit pedantic about
it!?! : )


Well, the best I can do to achieve what I think you want:
Call an arbitrary function with an arbitrary amount of arguments, switching
scalars and arrays like it doesn't care, the first argument given is the
function, be it in a scalar or the first scalar in the first array (of the
first array and so forth):

<?php

function flatten_array($arg){
if(!is_array($arg)) return array($arg);
$return = array();
foreach($arg as $row){
if(is_array($row));
$return = array_merge($return,flatten_array($row));
} else {
$return[] = $row;
}
}
return $return;
}

function varargsfunction(){
$array = func_get_args();
$array = flatten_array($array);
$function = array_shift($array);
return call_user_func_array($function,$array);
}
?>

Grtz,
--
Rik Wasmus
Aug 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
7
by: confusedcoder | last post by:
I found a strange piece of code that basically did the following: int doSomething(int& foo) { //manipulate the value of foo return foo; } int main(int argc, char* argv) {
1
by: Ben Kial | last post by:
I'd like to write a wrapper function "mysprintf(char *buffer, char *format, ....)" which calls sprintf(). My question is how can I pass variable argument in mysprintf() to sprintf(). Thanks in...
1
by: S?ren Gammelmark | last post by:
Hi I have been searching the web and comp.lang.c of a method of using variable-argument function pointers and the like. And some of the questions arising in this post are answered partly in...
4
by: thomasp | last post by:
In the code below I have a function that tests if a file exists. It takes a variable named strFileName, simple enough. My question is, is there a way to pass it a variable with another name as...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
9
by: Schraalhans Keukenmeester | last post by:
I have some C functions (with variable length argument lists) that use void pointers as arguments. Is there a way to determine at runtime what type of parameter is actually passed on to the...
2
by: Agile | last post by:
I have a simple function that takes variable argument list, the sample code is below: func1( char* a, ...) { va_list arg_list; va_start(arg_list,a); // do something with arg_list
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.