answersLogoWhite

0

How are default parameters useful?

Updated: 12/11/2022
User Avatar

Wiki User

14y ago

Best Answer

C itself does not support default values for function arguments, if that is your definition of parameter.

In languages that do, like C++/python/ and Java (presumably) you can use default parameters as a rudimentary form of polymorphism in the sense that you can call the function or method with a minimal set of arguments assuming the that others are defaulted to the values you want, this can be actual defaulted values like the number -1, or sentinel values that omit functionality in the function/method you are calling.

You will, in most of these languages that support this, have to arrange the position of the defaulted arguments to the end of the argument list, and in doing so it would be best to prioritize them from least likely to use the default value to the most from left to right. This in design time can conflict with function/method overloading where you vary the number of arguments in the prototypes of your overloaded function, so I consider these two features of a language mutually exclusive, i.e. don't use them together unless you have a good reason.

This feature is rarely useful but in saying "rarely" when it is, it is the most helpful. I find it and so do the creators of the C++ libraries helpful for class constructors when sometimes you need to set some internal features during construction time.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How are default parameters useful?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are default arguments in c plus plus?

Default arguments are function parameters for which a default value is implied when not explicitly stated. int foo(int x, int base=10 ) { return( x%base); } The above function assumes 'base' is 10 unless you specify otherwise when making the call. Thus calling foo(15) will return 5, as will foo(5,10), but foo(15,16) will return 15. Note that default parameters must appear after all non-default parameters in a function declaration. Once you specify a default parameter, all other parameters that follow must also have default values. Note also that when the definition of a function is split from its declaration, only the declaration should declare the default parameters: // Declaration: int foo(int x, int base=10 ); // Definition: int foo(int x, int base ) { return( x%base); }


What is the difference between default constructor and parameterized constructor?

A default constructor is one that has no parameters (C++ also calls constructors with all default parameters a default constructor), while a parameterized constructor is one that has at least one parameter without a default value. Default constructors can be provided by the compiler if no other constructors are defined for that class or any class the class inherits from, while parameterized constructors must always be defined by the developer.


Can attributes be placed on specific parameters to a method and Why is this useful?

Yes


A default constructor has how many parameters?

None, zero. Example: new MyClass(); //and public classMyClass{} no constructor defined inside of MyClass


Why are constructors dynamic rather than static by default?

Because the JVM decides are run time which constructor to invoke depending on the parameters passed to it

Related questions

What is a default parameterized constructor?

There is no such thing as a default parameterized constructor. The default constructor is always the 'no-arg' constructor and does not take any parameters or arguments as input


What is default value of formal arguments?

In C, there is no default value for formal parameters. In C++, there can be, but the value is whatever you declare in the function declaration.


What are default arguments in c plus plus?

Default arguments are function parameters for which a default value is implied when not explicitly stated. int foo(int x, int base=10 ) { return( x%base); } The above function assumes 'base' is 10 unless you specify otherwise when making the call. Thus calling foo(15) will return 5, as will foo(5,10), but foo(15,16) will return 15. Note that default parameters must appear after all non-default parameters in a function declaration. Once you specify a default parameter, all other parameters that follow must also have default values. Note also that when the definition of a function is split from its declaration, only the declaration should declare the default parameters: // Declaration: int foo(int x, int base=10 ); // Definition: int foo(int x, int base ) { return( x%base); }


What is the difference between default constructor and parameterized constructor?

A default constructor is one that has no parameters (C++ also calls constructors with all default parameters a default constructor), while a parameterized constructor is one that has at least one parameter without a default value. Default constructors can be provided by the compiler if no other constructors are defined for that class or any class the class inherits from, while parameterized constructors must always be defined by the developer.


Can attributes be placed on specific parameters to a method and Why is this useful?

Yes


Which Selection Criteria Are Parameters Saved Settings For?

Vendor Data, Default Data, and Profile Data


A default constructor has how many parameters?

None, zero. Example: new MyClass(); //and public classMyClass{} no constructor defined inside of MyClass


Why are constructors dynamic rather than static by default?

Because the JVM decides are run time which constructor to invoke depending on the parameters passed to it


Can the DHCP client be enbled to receive other IP Parameters besides.?

A DHCP client can receive about 30 different parameters, including the IP address, the subnet mask (which indicates the size of the local network), the next-hop address or default router, the DNS servers, and several parameters which are specific to Windows networks.


What is the function of plus sign?

The "plus sign" (+) is an operator that, by default, takes the left and right operands as parameters, and returns the sum of both operands as the return value.


What is the need of parameterized constructor?

Parametrized constructors are used to increase the flexibility of a class, allowing objects to be instantiated and initialised in more ways than is provided by the default and copy constructors alone. If you define any parametrized constructor, including a copy constructor, you will lose the default constructor generated by the compiler and must declare your own if you need one. The default constructor can also be parametrized, but each parameter must include a default value in the declaration, so that it can be called without any parameters.


Which method is default by true in java?

I don't understand what you mean with "default by true"? Please clarify your question. A Java method can have zero or more parameters, and it must have one return value. Any of these can be declared as "boolean", in which case the value can be either true or false.