This question already has answers here:
Default parameter values must appear on the declaration, since that is the only thing that the caller sees.
EDIT: As others point out, you can have the argument on the definition, but I would advise writing all code as if that wasn't true. Default Arguments in C++, Default arguments are used in place of the missing trailing an argument for which a default is already visible (even if the value is the same). /* in the header file */ #ifdef __cplusplus /* in case the compiler is a C++ compiler */ #define DEFAULT_VALUE(value) = value #else /* otherwise, C compiler, do nothing */ #define DEFAULT_VALUE(value) #endif void window_set_size(unsigned int width DEFAULT_VALUE(640), unsigned int height DEFAULT_VALUE(400));
You can do either, but never both. Usually you do it at function declaration and then all callers can use that default value. However you can do that at function definition instead and then only those who see the definition will be able to use the default value.
Default arguments - cppreference.com, From C++ we know the concept of default arguments for functions. often such that really the value of the call to NARG2 is put in place. Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++14) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.
C++ places the default parameter logic in the
calling side, this means that if the default value expression cannot be computed from the calling place, then the default value cannot be used.
Other compilation units normally just include the declaration so default value expressions placed in the definition can be used only in the defining compilation unit itself (and after the definition, i.e. after the compiler sees the default value expressions).
The most useful place is in the declaration (.h) so that all users will see it.
Some people like to add the default value expressions in the implementation too (as a comment):
void foo(int x = 42,
int y = 21);
void foo(int x /* = 42 */,
int y /* = 21 */)
However, this means duplication and will add the possibility of having the comment out of sync with the code (what's worse than uncommented code? code with misleading comments!).
Default arguments for C99 – Jens Gustedt's Blog, Once we provide a default value for a parameter, all subsequent parameters must also have default values. For example, // Invalid void add(int a, int b = 3 In the above program, you can see the default value assigned to the arguments void display (char = '*', int = 1);. At first, display () function is called without passing any arguments. In this case, display () function used both default arguments c = * and n = 1. Then, only the first argument is passed using the function second time.
C++ Programming Default Arguments (Parameters), In the first case the value for the argument called c is useful when one wants to set default criteria so that the Rule: If the function has a forward declaration, put the default argument there. Otherwise, put them in the function definition. Default arguments and function overloading. Functions with default arguments may be overloaded. For example, the following is allowed:
If the functions are exposed - non-member, public or protected - then the caller should know about them, and the default values
must be in the header.
If the functions are private and out-of-line, then it does make sense to put the defaults in the implementation file because that allows changes that don't trigger client recompilation (a sometimes serious issue for low-level libraries shared in enterprise scale development). That said, it is definitely potentially confusing, and there is documentation value in presenting the API in a more intuitive way in the header, so pick your compromise - though consistency's the main thing when there's no compelling reason either way.
Default argument, If you pass only one argument like this: sum(80) then the result would be 100, using the passed argument 80 as first value and 20 taken from the default Always we need to overload the method which has an optional parameter. Also if you are working with functions having more than one optional parameter, then it's good to pass the value using the name of the parameter. function Add(int a, int b, int c = 0); It's always good to call this function using the following way. Add(10, 20, c:30); Default Arguments in C++ Functions, Parameters can provide default values to simplify function calls and can be You can also optionally define a type of value that the function will pass back as Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. A default value must be one of the following types of expressions: a constant expression; an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct; Functions, In the code above, we set the default parameter value of b to 1 , so if the c = 1; } return a+b+c; }. If we call the following sum function without Output: In func, a = 12 b = 7 In main, x = 5 y = 7 Languages like C, C++, Java support this type of parameter passing. Java in fact is strictly call by value.
Comments +1: even though you're technically allowed to choose, doing it in the declaration is the only self-documenting way. I wonder if there are any cases where it would make sense for the defaults be implementation-specific. Which makes me wonder something else: Is it possible to mix it up? Like have one parameter have a default set in the implementation, and one in the declaration? Not sure why you'd want to do that. But still curious. @AidanMueller That's not possible. The default is passed by the caller based on the declaration it saw, not the value specified in the definition. That may be technically correct, but I wouldn't consider it good advice. If you want to be REALLY horrible, you can actually do both, but for different parameters. :-) @Bo Persson: Great idea. Except we already have templates for being as horrible as one wants. @sharptooth: and macros :) doing either for me produces the following g++
error: default argument given for parameter <x> of <fun(args)> [-fpermissive] ..... after previous specification in <fun(args)> [-fpermissive]
I don't have a problem with these comments, since 99.99% of the time the parameter is simply value-initialized. Cannot we assign the parameter to an object by default? like
void test(obj, a, b = a);
@XStylish Yes, as long as it is not a static object. Relevant language:
[..] In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration [..]