Question:
default arguments in c++?
NotMe
2010-07-08 18:11:31 UTC
i posted a question on homework help.. its regarding a c++ program "function overloading , default arguments" i understood what is function overloading but i need to know about the 'default arguments' part. anyone help soon. please.......
Six answers:
ʃοχειλ
2010-07-09 01:33:50 UTC
A default argument is a part of function declaration (not definition - in function definition, it must always be provided). It instructs the compiler what value to pass for an argument if the programmer deliberately misses the argument when calling a function.



As a reasonable example, suppose we define a function which parses a text string to extract an integer value; that is, converting "123" to 123. The function definition is like:



int parseInt (string numberText, int radix)

{

//...

}



Here, the second parameter is the radix of conversion: 2, 8, 10, 16. This function assumes that this value is provided and uses it as the radix of the number.



However, in declaration, you could assume that the default radix is 10 (the value taken if not explicitly given by user) and declare it as:



int parseInt (string numberText, int radix = 10);



This instructs the compiler that the programmer could deliberately not give the second argument (radix) in which case, the compiler should assume radix = 10. In other words, the programmer writes the code like:



int value = parseInt ("22331");



and the compiler would convert it to



int value = parseInt ("22331", 10);



automatically before translating it to the machine code.



A final important note is that a default argument must be provided such that it does not produce ambiguity with already existing function overloads. As an example, suppose we have max () functions which returns the maximum number of the given arguments:



max (int a, int b); // returns the maximum of a and b.

max (int a, int b, int c); // returns the maximum of a, b and c.



Here we cannot make the argument c of the second function as a default parameter:



max (int a, int b, int c = INT_MIN); // returns the maximum of a, b and c (if present).



if max (int, int) has not been defined as overload, this function, max (int,int,int), would return the maximum of a and b because, with c = INT_MIN, c would always lose all competitions, and actually only a and b are compared.



But since the overload max (int,int) does exist, the default argument has some ambiguity and incurs compiler error:



cout << max (20, 25); // trouble



which overload? max (int, int) or max (int, int, int) with default INT_MIN?
auldridge
2016-11-06 04:17:10 UTC
Default Arguments
sougata s
2010-07-08 18:27:29 UTC
include

using namespace std;



int a = 1;

int f(int a) { return a; }

int g(int x = f(a)) { return x; }



int h() {

a = 2;

{

int a = 3;

return g();

}

}



int main() {

cout << h() << endl;

}



This example prints 2 to standard output, because the a referred to in the declaration of g() is the one at file scope, which has the value 2 when g() is called.



The default argument must be implicitly convertible to the parameter type.



A pointer to a function must have the same type as the function. Attempts to take the address of a function by reference without specifying the type of the function will produce an error. The type of a function is not affected by arguments with default values.



The following example shows that default arguments are not considered part of a function's type. The default argument allows you to call a function without specifying all of the arguments, it does not allow you to create a pointer to the function that does not specify the types of all the arguments. Function f can be called without an explicit argument, but the pointer bad pointer cannot be defined without specifying the type of the argument:





int f(int = 0);

void g()

{

int a = f(1); // ok

int b = f(); // ok, default argument used

}

int (*pointer)(int) = &f; // ok, type of f() specified (int)

int (*badpointer)() = &f; // error, badpointer and f have

// different types. badpointer must

// be initialized with a pointer to

// a function taking no arguments.



C++ allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function. For example, in the following function declaration:



int MyFunc(int a, int b, int c=12);





This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways:



1.result = MyFunc(1, 2, 3);

2.result = MyFunc(1, 2);







In the first case the value for the argument called c is specified as normal. In the second one, the argument is omitted, and the default value of 12 will be used instead.



There is no means to know if the argument has been specified by the caller or if the default value was used.



Some other languages, like Java, do not have default arguments. However, the same behavior can be simulated by using method overloading to create overloaded methods of the same name, which take different numbers of arguments; and the versions with fewer arguments simply call the versions with more arguments, with the default arguments as the missing arguments:





int MyFunc(int a, int b) { return MyFunc(a, b, 12); }

int MyFunc(int a, int b, int c) { /* main implementation here */ }
The Phlebob
2010-07-08 18:21:13 UTC
A default argument in C++ is (if I remember the syntax) something like this:



int function abc( int param1, bool param2 = false, int param3 = 0 );



Here, param1 must be provided in any calls to function abc(), but param2 and param3 can be left off (incrementally) and their default values of false and 0, respectively, will be used instead.



Note that this can be duplicated with function overloading something like this:



int function abc( int param1, bool param2, int param3 ); // Note: No default values

int function abc( int param1 )

{

return ( abc( param1, false, 0 ));

}

int function abc( int param1, bool param2 )

{

return ( abc( param1, param2, 0 ));

}



I think the default parameters have to be the last ones in the calling sequence, and you can't skip one in a call. In other words,



abc( 99, , 10 ); won't work.



As a practical matter, defaults are often used in two situations:



1. When some parameters are so often one specific value that it's not worth having to specify them on all calls or,

2. When a calling sequence is being modified after much development and new parameters are being added. Rather than modify every instance of a call to the function, defining the new parameters with defaults, if possible, is a quick shortcut.



Hope that helps.
anonymous
2016-12-18 08:44:37 UTC
by way of default fee we recommend that if the person do no longer grant any fee the default values must be used now how can this be available that a value is used as a default yet remains being entered by way of the purchasers (default fee)
?
2016-12-07 14:50:40 UTC
by default value we advise that if the person do no longer furnish any value the default values must be used now how can this be achieveable that a cost is used as a default yet continues to be being entered by the customers (default value)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...