answersLogoWhite

0


Best Answer

We declare (not use) default arguments in a function whenever the default values cover the majority of calls to that function. We use default arguments in order to simplify those calls and thus reduce the verbosity of our calling code, thus making it easier to call the function.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

You don't need to use default arguments in a function -- they are optional. You use them if and when they are required.

Typically you will use them to gain the benefit of function overloading without the need to provide completely separate implementations.

As an example, consider the following trivial class declaration:

struct foo

{

foo(): m_integer(0), m_floating(0.0) {}

foo(int integer, float floating): m_integer(integer), m_floating(floating) {}

private:

int m_integer;

float m_floating;

};

Note that there are two constructors, a default constructor that accepts no arguments and an overloaded constructor that accepts two arguments. In both cases the implementations are fundamentally the same (they both initialise the data members) so they could just as easily be combined into a single constructor with default arguments:

struct foo

{

foo(int integer=0, float floating=0.0): m_integer(integer), m_floating(floating) {}

private:

int m_integer;

float m_floating;

};

Now you gain the benefit of construction overloading via a single constructor. In effect you've actually created 4 different construction methods, depending on which, if any, arguments you supply:

foo a; // assigns 0 to integer and 0.0 to floating

foo b(1); // assigns 1 to integer and 0.0 to floating

foo c(1,1.0); // assigns 1 to integer and 1.1 to floating

foo d(,1.0); // assigns 0 to integer and 1.0 to floating

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

When you want to. Its as simple as that. You are the designer, and you choose if you want functions to have default arguments.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When do you need to use default arguments in a function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What parameter to a function is one for which an automatic value is supplied if do not explicitly use one in C programming?

You are referring to default arguments. However, C does not support default arguments. That's a C++ feature.


Where to use overloading?

Default arguments are often considered to be optional arguments, however a default argument is only optional in the sense that the caller need not provide a value for it. The function must still instantiate the argument and must assign the appropriate value to it so, insofar as the function is concerned, the argument is not optional. To implement a function with a truly optional argument, we can define two overloads of that function, one that accepts the optional argument (without specifying a default value) and one that does not accept the argument. In this way we can define two different implementations, one that uses the argument and one that does not. void f (); // implementation that does not use the argument void f (int); // implementation that does use the argument In many cases, a default argument incurs no significant overhead over that of overloading. Thus we'd only use overloading to implement an optional argument where there is a significant overhead incurred by a default argument. Even so, we must also be aware that by eliminating the overhead within the function itself we may simply be passing that overhead back to the callers, because some or all of them would then have to decide which overload to call, resulting in code duplication that would likely be best handled by the function itself.


Can you use arguments in main function?

Yes.


How are default parameters useful?

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.


C plus plus Error Too many arguments in function call?

The function prototype (declaration) determines the number and type of arguments a function will accept. If the number or type of arguments passed to a function do not agree with its prototype, the compiler will notify you of the error. That is, if the function only accepts one parameter, you cannot call the function by passing two or more arguments, since no such prototype exists. The compiler makes a best guess on which function you were trying to call (by the name you provided) and notifies you that the number or type of arguments do not agree with the available prototypes. If the function is your own function, you can include the additional parameters as default values and re-implement the function to make use of those parameters, or you can overload the function to provide a completely new implementation that accepts the additional parameters. The new implementation may call the original implementation and embellish that implementation with its own implementation, or it can provide a completely separate implementation. Note that no two functions can have the same name and signature within the same namespace. Every prototype must be unique and cannot differ by return type alone. That is, the number and/or type of arguments must differ in some way, with no ambiguity, so the compiler knows which function you are actually calling (as determined by the prototype).

Related questions

What parameter to a function is one for which an automatic value is supplied if do not explicitly use one in C programming?

You are referring to default arguments. However, C does not support default arguments. That's a C++ feature.


When to use optional arguments in Excel?

Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.Optional arguments are ones in a function that you don't have to use. The function will still work without them. It will depend on the function you are using and what you are trying to achieve as to when you use them. Check the help on particular functions that have optional arguments and see what they do. Then experiment with them. They usually give extra information or change how the function works or include extra values. Sometimes you will be able to use them, and other times they are not necessary or a default value is taken.


Do all functions have arguments?

Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.Some functions do and some don't. So it depends on which function you use. Some need more information, like ones that are calculating something like SUM or AVERAGE. Others do not need any arguments like the TODAY function which gives the current date on the computer. So for each function you need to check how to use it and as part of that you will have to find out if it uses arguments and the type and amount of arguments it uses.


Where to use overloading?

Default arguments are often considered to be optional arguments, however a default argument is only optional in the sense that the caller need not provide a value for it. The function must still instantiate the argument and must assign the appropriate value to it so, insofar as the function is concerned, the argument is not optional. To implement a function with a truly optional argument, we can define two overloads of that function, one that accepts the optional argument (without specifying a default value) and one that does not accept the argument. In this way we can define two different implementations, one that uses the argument and one that does not. void f (); // implementation that does not use the argument void f (int); // implementation that does use the argument In many cases, a default argument incurs no significant overhead over that of overloading. Thus we'd only use overloading to implement an optional argument where there is a significant overhead incurred by a default argument. Even so, we must also be aware that by eliminating the overhead within the function itself we may simply be passing that overhead back to the callers, because some or all of them would then have to decide which overload to call, resulting in code duplication that would likely be best handled by the function itself.


Can you use arguments in main function?

Yes.


How are default parameters useful?

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.


What are the two parts required to use a function in a program?

function name and arguments


How do you write a function with a variable number of input elements in PHP?

When defining your function, do not put any arguments in the definition (e.g. function myFuntion()). Inside the function, you can use func_num_args() and func_get_arg($number) to get the function's arguments.


What does Excel use to separate multiple arguments in a function?

Commas are used to separate arguments in Excel functions.


The values that you use with a function are called?

values used with a function are called


When does the function arguments box appear in Excel?

It shows you what needs to be put into a function for it to work. You can type the arguments directly into the boxes that are shown for the particular function you are using. It is particularly handy for more complex functions that you are not used to working with.


How do you pass the parameter in applet?

If you have the function main()... You can use its arguments to pass information.