473,320 Members | 2,092 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,320 software developers and data experts.

How to call a function with variable argument list...


How to call a function with variable argument list from another function
again with variable argument list?

Example :

double average ( int num, ... );
double AFunct1 ( int num, ... );
double AFunct2 ( int num, ... );

double average ( int num, ... )
{
va_list arguments; // A place to store the list of
arguments
double sum = 0;

va_start ( arguments, num ); // Initializing arguments to store
all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument
list to sum.
va_end ( arguments ); // Cleans up the list

return sum / num; // Returns some number (typecast
prevents truncation)
}

double AFunct2 ( int num, ... )
{
HOW to call "avarge" here? <----???????
}

double AFunct1 ( int num, ... )
{
HOW to call "AFunct2" here? <----???????
}

main ()
{
double d = AFunct1(5, 2, 3, 4, 123.456, 78);
cout << d;
}
Jul 23 '05 #1
7 8292
<Giatto Cardiacci> wrote:

How to call a function with variable argument list from another function
again with variable argument list?
There is no way to do that.
Example :

double average ( int num, ... );
double AFunct1 ( int num, ... );
double AFunct2 ( int num, ... );

double average ( int num, ... )
{
va_list arguments; // A place to store the list of
arguments
double sum = 0;

va_start ( arguments, num ); // Initializing arguments to
store
all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are
added
sum += va_arg ( arguments, double ); // Adds the next value in
argument
list to sum.
va_end ( arguments ); // Cleans up the list

return sum / num; // Returns some number (typecast
prevents truncation)
}

double AFunct2 ( int num, ... )
{
HOW to call "avarge" here? <----???????
}

double AFunct1 ( int num, ... )
{
HOW to call "AFunct2" here? <----???????
}

main ()
{
double d = AFunct1(5, 2, 3, 4, 123.456, 78);
cout << d;
}


Make a version of your average function that takes a va_list.

double vaverage(int num, va_list arguments)
{
double sum = 0;
for (int x = 0; x < num; x++)
sum += va_arg(arguments, double);
return sum / num;
}

double average ( int num, ... )
{
va_list arguments;
va_start(arguments, num);
double sum = vaverage(num, arguments);
va_end(arguments);
return sum;
}

double AFunct2(int num, ...)
{
//use vaverage instead of average
}

Generally, it's best to avoid variable argument lists in C++, since they are
not type safe and very restrictive and error-prone.

Jul 23 '05 #2
Giatto Cardiacci schrieb:
How to call a function with variable argument list from another function
again with variable argument list?

Example :

double average ( int num, ... );
double AFunct1 ( int num, ... );
double AFunct2 ( int num, ... );

double average ( int num, ... )
{
va_list arguments; // A place to store the list of
arguments
double sum = 0;

va_start ( arguments, num ); // Initializing arguments to store
all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument
list to sum.
va_end ( arguments ); // Cleans up the list

return sum / num; // Returns some number (typecast
prevents truncation)
}

double AFunct2 ( int num, ... )
{
HOW to call "avarge" here? <----???????
}

double AFunct1 ( int num, ... )
{
HOW to call "AFunct2" here? <----???????
}

main ()
{
double d = AFunct1(5, 2, 3, 4, 123.456, 78);
cout << d;
}


Why not use a std::vector<double> to store the numbers to be averadged.

Stefan
Jul 23 '05 #3
Why not use a std::vector<double> to store the numbers to be averadged.

Stefan

The function "avarage" was just for an example.
What I want to do is to be able to send variable number of parameters of
variable type to a function (simiar to good-old printf function).
Jul 23 '05 #4
"Giatto Cardiacci" wrote:
Why not use a std::vector<double> to store the numbers to be averadged.

Stefan


The function "avarage" was just for an example.
What I want to do is to be able to send variable number of parameters of
variable type to a function (simiar to good-old printf function).


Even in this case I would try to come up with a container that holds
all those arguments.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #5
Karl Heinz Buchegger schrieb:
"Giatto Cardiacci" wrote:
Why not use a std::vector<double> to store the numbers to be averadged.

Stefan


The function "avarage" was just for an example.
What I want to do is to be able to send variable number of parameters of
variable type to a function (simiar to good-old printf function).

Even in this case I would try to come up with a container that holds
all those arguments.


Right.
Or, if there's only a limited range of number of arguments (i.e. 2..5
arguments) use function overloading.

Stefan
Jul 23 '05 #6
<Giatto Cardiacci> wrote:

How to call a function with variable argument list from another function
again with variable argument list?


something like that worked for me:

double average ( int num, va_list args )
{
for (...)
{
sum += va_arg(args)
}
return sum;
}

double AFunct1 ( int num, ... )
{
va_list args;
va_start(args, num);
ret = average(num, args);
va_end(arrgs);
return ret;
}

Michael

Jul 23 '05 #7
Michael Wohlwend wrote:
{
sum += va_arg(args)


please ignore error like that :-)

should be:
sum += va_arg(args, double);
Jul 23 '05 #8

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

Similar topics

1
by: sam | last post by:
I have a function writen in Perl that takes a name-value list (array) as argument and returns a name-value list (array). My question is: How to call this function from PHP and get the returned...
9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
77
by: Eltee | last post by:
Hi everybody, Is it possible to 1. call a function from a dll made with .NET (C#) 2. from a program written in plain (as in: not .NET) C or C++? To be more specific, this is what I have. ...
6
by: enjoying the view | last post by:
I am working on a school project, trying to build a simple RPC stub generator. The idea is that the generator takes a normal python file with functions defined in it and produces a client and...
1
by: sphinx.moro | last post by:
Hello, I have a function which is coded to receive an unknown number of arguments void Process(int i_ss, int Nerr, ...) { va_list pa; /* variable argument list*/ va_start(pa,...
9
by: Allen | last post by:
The arguments of function is variable. Given function address, argument type and data, how to dynamically call the function? The following is pseudo code. int count = 0; int offset = 0; char...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.