I have some questions about the default values in a function parameter list
-
Is the default value a part of the signature? What about parameter type of the default parameters?
-
Where are the default value stored? In the stack or or global heap or in the constant data segment?
No, default argument is not a part of signature and is not a part of the function type.
Parameter type is a part of signature. But default argument type has no effect of parameter type, i.e default argument type has no effect on signature.
Default arguments are not “stored” anywhere specifically. Default arguments are “syntactic sugar” that exists (as default arguments) only during the program’s compilation. If during the compilation compiler notices that some argument is missing, it will use the default argument, as specified by you. The evaluation of the default argument is done in the context of the caller. If you specify a temporary object as a default argument, a separate temporary will be created every time you call the function using the default argument and destroyed immediately after the calling expression ends.
If you specify an existing object with static storage duration as a default argument, then it will be stored wherever you define it.
If you declare default arguments but never actually use them, i.e. if you specify the arguments explicitly every time, then the compiled program will have no trace of these arguments whatsoever (which is why I called them compile-time “syntactic sugar”).
P.S. To include what Johannes says in the comment below: even though the default argument (when used) is evaluated in the context of the caller at the moment of the call, it is not done by “textual substitution” as in might appear from my examples above. Most notably, the name lookup for the names used in default arguments is done at the point when the default argument is specified in the function declaration, not at the point of the evaluation in the caller.