Named parameters: Difference between revisions

Added C++ example.
(→‎{{header|JavaScript}}: print ~> alert)
(Added C++ example.)
Line 53:
WinWaitClose, %A_ScriptFullPath%
}</lang>
 
=={{header|C++}}==
C++ does not support name parameters, but they can be simulated with the "Named Parameter Idiom" (http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18).
 
You wrap the parameters in a class, and make the function a friend of the parameter class.
 
<lang c++>class foo_params{
friend void foo(foo_params p);
public:
foo_params(int r):
required_param_(r),
optinal_x_(0),
optinal_y_(1),
optinal_z_(3.1415)
{}
foo_params& x(int i){
optinal_x_=i;
return *this;
}
foo_params& y(int i){
optinal_y_=i;
return *this;
}
foo_params& z(float f){
optinal_z_=f;
return *this;
}
private:
int required_param_;
int optinal_x_;
int optinal_y_;
float optinal_z_;
};</lang>
 
Declare the function to take the parameter class as its only paramiter.
 
<lang c++>void foo(foo_params p){ . . .}</lang>
 
Call the function using the parameter object constructor with the required parameters and chaining the optional parameters.
 
<lang c++>foo(foo_params(42).x(7).z(23.54));</lang>
 
=={{header|Clojure}}==
Line 744 ⟶ 785:
 
{{omit from|C}}
{{omit from|C++}}
{{omit from|Java}}
{{omit from|Joy}}
Anonymous user