Options (arguments) the default function.

c ++ defaults, c ++ default arguments We have already talked about function parameters in a separate article. In this – In short, we will understand, what the default settings function. It is easy to understand. Their use in programming primarily for convenience. For example we need to draw 5 identical rectangles. To do this, we can write a function, that takes the default settings. She draws, let's say, rectangle 10 on 10 characters. And this character is always ‘@’:

Although we do not pass any parameters in the function when calling, we will see five identical rectangles of characters ‘@’ on our screen. Function used those values, that have been assigned default settings when determining.

But that's not all. Even if we have defined the default settings – There is a convenient opportunity to modify their values when calling.

Let's, it is necessary to draw a rectangle 5 characters in width. Other parameters (10 characters in height and the character ‘@’ ) we arrange. Then we pass to the function only one parameter. The remaining options will function from the default.

c ++ defaults, c ++ default arguments

If we need to change only the character and the parameters it is listed last, you will need to write the preceding parameters, even if they are satisfied with us.

c ++ defaults, c ++ default arguments

On the screen see:

c ++ defaults, c ++ default arguments

Another point, which relates to the definition of the default settings in the function header. Defines they have the right to the left. For example, if three of the parameters must be set only one default, It should be defined as an extreme right.

with default parameters ++, arguments default c ++, c++

In the picture the last default setting : char symb = ‘@’. If it is necessary to define two – the same way they would be two extreme right:

If you declare a function prototype – it is necessary to define the default settings it is in the prototype. The definition of functions do not have to do this. Perhaps everything. If something is not clear – ask in the comments.

Newsletter of programming:

Options (arguments) The default C ++ function in

One thought on “Options (arguments) the default function.

  1. The default settings are usually like that just do not apply… They are needed if there are some typical parameters. well, for example…

    You write books reader, and default scale 100%. Perhaps you will be the type of function
    load(string filename, /* all other parameters */, scale = 100);
    That is. understandably, the filename – mandatory parameter, and the scale can not be specified.

    Write you text editor. Encoding the default UTF-8 (for example).

    Write Unit, which stores anything in XML format (there is such a popular format) and the default XML version 1.0.

    I default settings in their functions almost do not use. Who looked for them in his latest project. I found only one example:

    void animation(QString texturename, bool randomStartFrame = false);

    Here the function starts the animation, in the first argument passed to the file name with animation. If the second argument is not specified, the default animation is started from the first frame. If the second argument is true – the animation will be launched from a random frame.

    As for me, But in this case, the animation is also the default settings to use would not be worth… In any case, their use should be justified “normal behavior”, from which you can deviate specifying parameters.

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>