Functions. Default arguments in functions
Contents
- 1. What is the default argument? How are default arguments declared? The general form of declaring a function that contains the default arguments
- 2. Examples of declaring and using functions that use the default arguments
- 3. Can a class constructor contain default parameters?
- 4. An example of using a constructor and a class method that receive default arguments
- 5. What are the advantages of using default arguments in C++ programs?
- 6. How many times it is possible to specify the default arguments when declare a function? An example
- 7. In what order should the default arguments follow with respect to the usual arguments?
- Related topics
Search other websites:
1. What is the default argument? How are default arguments declared? The general form of declaring a function that contains the default arguments
The default argument is an argument to the function that the programmer may not specify when the function is called. The default argument is added by compiler automatically.
The default argument is added by compiler automatically. The default arguments are declared in the function prototype.
The general form of the function declaration that contains the default arguments
returned_type FunName(type1 v1 = val1, type2 v2 = val2, ..., typeN vN = valN) { // ... }
where
- returned_type – the type, which function returns;
- FunName – function name;
- type1, type2, …, typeN – types of variables v1, v2, …, vN;
- val2, …, valN – values, which are assigned by default to variables v2, …, vN. In this case the types and the assigned values must be compatible. If in the function declaration in the parameter list there is an entry like
type v = val
then the argument v is the default argument (here type-some type, v is a type variable, val is the value that is assigned by default to the variable v).
2. Examples of declaring and using functions that use the default arguments
Example 1. Declaring the function Inc(), which receives two parameters. The function returns the sum of the values of the first and second parameters. The second parameter of the function is the parameter (argument) by default, which is set to 1.
The text of the Win32 Console Application, which demonstrates the use of the Inc() function, looks like
#include "stdafx.h" #include <iostream> using namespace std; // function containing the default argument int Inc(int value, int step = 1) { return value + step; } int _tmain(int argc, _TCHAR* argv[]) { int v1, v2; v1 = 5; // the use of default argument v2 = Inc(v1); // v2 = 5 + 1 = 6 v1 = 5; v2 = Inc(v1, 3); // v2 = 5 + 3 = 8 cout << v2 << endl; return 0; }
Example 2. The AreaTriangle() function is realized, that calculates the area of the triangle specified by the lengths of the sides a, b, c. The function receives parameters that have default values which are equal 1.
The listing of the declaration and use of the function is as follows.
#include "stdafx.h" #include <iostream> using namespace std; // Area of a triangle // function receives arguments by default double AreaTriangle(double a = 1, double b = 1, double c = 1) { double p, s; if ((a+b)<c || (b+c)<a || (a+c)<b) return 0; p = (a + b + c)/2.0; // semiperimeter s = ::sqrt(p * (p-a) * (p-b) * (p-c)); // Heron's formula return s; } int _tmain(int argc, _TCHAR* argv[]) { double area; area = AreaTriangle(); // area = 0.433013 area = AreaTriangle(1.5); // area = 0.496078 area = AreaTriangle(1.5, 1.5); // area = 0.707107 area = AreaTriangle(1, 1, 1); // area = 0.433013 area = AreaTriangle(2, 2, 2); // area = 1.7325 area = AreaTriangle(2, 2, 8); // area = 0 cout << area << endl; return 0; }
3. Can a class constructor contain default parameters?
Yes.
4. An example of using a constructor and a class method that receive default arguments
Let the class Circle, realizing a circle on the coordinate plane, be given. In the class are declared:
- internal hidden (private) variables named x, y, radius;
- a constructor that contains three default arguments;
- methods for reading the values of internal variables GetX(), GetY(), GetR();
- a method for establishing new values of internal variables SetXYR(). This method receives the default parameters.
#include "stdafx.h" #include <iostream> using namespace std; // class that implements the circle class Circle { // internal variables of class double x, y, radius; public: // constructor that receives default arguments Circle(double nx=10, double ny = 10, double nradius = 1) { x = nx; y = ny; radius = nradius; } // access methods // reading of values x, y, r double GetX(void) { return x; } double GetY(void) { return y; } double GetR(void) { return radius; } // a method that contains default arguments void SetXYR(double nx=0, double ny=0, double nradius=1) { x = nx; y = ny; radius = nradius; } }; int _tmain(int argc, _TCHAR* argv[]) { Circle c1; // object of class Circle - default arguments nx=10, ny=10, nradius=1 Circle c2(5); // nx=5, ny=10, nradius=1 Circle c3(7, 8); // nx=7, ny=8, nradius=1 Circle c4(12, 14, 20); // nx=12, ny=14, nradius=20 double d; d = c1.GetX(); // d = 10 d = c1.GetR(); // d = 1 d = c2.GetX(); // d = 5 d = c3.GetY(); // d = 8 d = c4.GetR(); // d = 20 // method Circle::SetXYR() c1.SetXYR(2, 3, 4); // nx=2, ny=3, nradius=4 d = c1.GetX(); // d = 2 c2.SetXYR(20,22); // nx=20, ny=22 d = c2.GetY(); // d = 22; cout << d << endl; return 0; }
5. What are the advantages of using default arguments in C++ programs?
Using default arguments gives the following advantages:
- reduces the listing of the program code by avoiding writing unnecessary functions that perform the same work only with other values of the arguments;
- provides a simple, natural and effective programming style;
- in some cases, the default arguments are a shortened form of the function overload. This, in turn, improves the readability of the program code and simplifies the function call.
6. How many times it is possible to specify the default arguments when declare a function? An example
When a function is declared, the default arguments are set only once. There are two possible cases. First, if the function has a prototype and implementation. The second case, if the function does not have a prototype, but has only an implementation.
In the first case (with the prototype), the default argument is specified only in the prototype function (and not in the implementation).
In the second case, the default argument is specified in the function implementation, since there is no function prototype.
Example. Let there two function be given:
- function GetAreaCircle(), which calculates the area of the circle. This function has a prototype;
- function GetVolumeSphere(), which calculates the volume of a sphere. This function has no prototype.
The following code demonstrates the use of functions that contain default arguments. Important: the default arguments are declared only once. In the GetAreaCircle() function, the default arguments are declared in the prototype, because this function has a prototype. In the GetVolumeSphere() function, the default arguments are declared in the implementation, since the function has no prototype.
Code listing created using the Win32 Console Application template is as follows:
#include "stdafx.h" #include <iostream> using namespace std; const double Pi = 3.1415; // declaring the default arguments in the function prototype double GetAreaCircle(double r = 1); // function GetVolumeSphere() has no prototype, // therefore, the default argument is set directly in the implementation of double GetVolumeSphere(double r = 1) { return 4.0/3 * Pi*r*r*r; } int _tmain(int argc, _TCHAR* argv[]) { double area; double volume; area = GetAreaCircle(); // area = 3.1415 volume = GetVolumeSphere(); // volume = 4.18867 area = GetAreaCircle(2); // area = 12.566 volume = GetVolumeSphere(2); // volume = 33.5093 cout << area << endl; cout << volume << endl; return 0; } // function implementation - default arguments are already defined in the prototype double GetAreaCircle(double r) { return Pi * r * r; }
7. In what order should the default arguments follow with respect to the usual arguments?
The arguments (parameters) by default should follow after the declaration of the usual arguments. If you declare the default arguments first, and after them declare the usual arguments, then this will be an error.
Related topics
- 1. Function declaration. Actual and formal parameters (arguments). Passing arguments to a function by value and by reference. Function prototype
- 2. Functions and arrays. Passing a one-dimensional and multidimensional array to a function. Passing a structure and a class to a function