views:

175

answers:

6

So here's my question in the function declaration there is an argument and it is already initialized to a certain value. What are the procedures to call this function and use that default value, or is it just a way to document code, telling other programmers what you expect them to use as a value for the parameter? Thank you.

enum File
{
    XML = 0,
    PDF = 1,
};

Char *SetSection(const File = XML);
A: 

XML is the standard parameter. You can call it with SetSection(); (But SetSection(XML) or SetSection(PDF) are valid, too).

mre
+1  A: 

If you call

SetSection();

It will call SetSection(XML) instead. This is why the optional parameters have to be at the end of all parameters. If you don't provide enough parameters, it will use the default.

Tristram Gräbener
+3  A: 

It means that the function can be called without parameters in which case the default value, XML, will be used.

MatsT
+4  A: 

If I understand your question correctly, simply calling SetSection with no parameters will do.

SetSection();

The above call gets translated (for lack of a better term) to:

SetSection(XML);
0xfe
That exactly what my question was, awesome that is an interesting feature that I never knew about.
Apoc
+2  A: 

In this case, File is a default argument. Calling SetSection() without arguments will set the File argument to the default value specified in the declaration.

Vulcan Eager
A: 

What you are seeing in the declaration is a default argument value.

If you call SetSection(); it is the same as calling SetSection(XML);

You can also call SetSelection(PDF); or use any other valid parameter to override the default.

You may also be seeing the result of an incremental development which started with the function having no parameter and calls to the function scattered throughout the code. Then the alternative file type of PDF was introduced, but the prototype was changed to have a default value, which meant not having to change the existing call site.

quamrana