JakobCHR.com
 
Quick Navigation:
 
Personal:
 Go to Home
 MS Research
 PhD Research
 Curriculum Vitae

General:
 Linux

Soon to come:
 Matlab
 On-line Stores
 Cycling
 Medicine & Health
 LaTeX
 OOP & C++
 Sony PCM-R500 DAT


next up previous contents index
Next: Calculating the point of Up: Implementing function-functions Previous: Implementing function-functions   Contents   Index

The remedy--variable number of arguments

In C it is allowed for a function to have a variable number of arguments--in analogy with the printf standard function. The new strategy is illustrated by the following prototype for the non-linear solver.

        double solve2(double guess, double (*FCN) (double, va_list), ...);
In regard to this prototype we have the comments
.
The ellipsis in the end tells the compiler that solve2 can have n arguments, where $n \ge 2$. The arguments which follow FCN are parameters (ie fixed variables) which are passed to FCN in every call.

.
The function which we solve, FCN, has got a new prototype. In addition to the variable double element the function takes an input argument of type va_list which are a standard type in C. The parameters to the FCN function are passed via this argument.
The consequence of the new procedure for passing parameters to FCN is that f_alpha now has the prototype
        double f_alpha(double x, va_list vlist);

The drawbacks are seen clearly

.
The number of parameters and their types are hidden within vlist.

.
At compile time it is impossible to catch a mismatch between the parameters that f_alpha needs and the parameters supplied by the call to solve2.

.
The function call overhead is increased but if the function evaluation (manipulation) is not very simple (like a single return statement) the extra overhead is negligible.

If one wishes to have some check of the number of parameters at run-time one could pass the number of parameters to FCN (eg f_alpha) as the first parameter. This strategy is used in the source code written by the author.

In conclusion we may state that this last method seems preferable when compared to the former which uses global variables.


next up previous contents index
Next: Calculating the point of Up: Implementing function-functions Previous: Implementing function-functions   Contents   Index  
 
 
 
Revision 2.0, Copyright © 1999-2004 Jakob Christensen
http://www.JakobCHR.com
E-Mail: webmaster@JakobCHR.com
Top Quality
Developed with

Danish
Brain Power
Linux Powered!