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‘@’:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> using namespace std; // функция рисует прямоугольник void drawRectangle(int countSymb1 = 10, int countSymb2 = 10, char symb = '@') { for (int i = 0; i < countSymb1; i++) { for (int j = 0; j < countSymb2; j++) { cout << symb; } cout << endl; } cout << endl << endl; } int main() { setlocale(LC_ALL, "rus"); cout << "Пять прямоугольников:\n\n"; drawRectangle(); // не передаем в функцию никаких параметров drawRectangle(); drawRectangle(); drawRectangle(); drawRectangle(); return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> using namespace std; // функция рисует прямоугольник void drawRectangle(int countSymb1 = 10, int countSymb2 = 10, char symb = '@') { for (int i = 0; i < countSymb1; i++) { for (int j = 0; j < countSymb2; j++) { cout << symb; } cout << endl; } cout << endl << endl; } int main() { setlocale(LC_ALL, "rus"); cout << "Используем параметры по умолчанию.\n\n"; drawRectangle(); // не передаем в функцию никаких параметров cout << "Задаём другое количество символов в ширину, а количество\n" << "символов в высоту и сам символ оставим по умолчанию.\n\n"; drawRectangle(5); cout << "Меняем только символ.\n\n"; //drawRectangle(' |
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 only one parameter to the function. The function will take the remaining parameters from the default ones..
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.
On the screen see:
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.
In the picture the last default setting : charsymb=‘@’.If it is necessary to define two – the same way they would be two extreme right:
1 | void drawRectangle(int countSymb1,int countSymb2 = 10,char symb = '@') |
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.
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.
the online compiler does not work – throws an error on the given example