Using default arguments for function - C++ Function

C++ examples for Function:Function Parameter

Description

Using default arguments for function

Demo Code

#include <iostream>

int boxVolume(int length = 1, int width = 1, int height = 1);

int main(int argc, const char *argv[]) {
    // no arguments--use default values for all dimensions
    std::cout << "The default box volume is: " << boxVolume();

    // specify length; default width and height
    std::cout << "\n\nThe volume of a box with length 10,\n" << "width 1 and height 1 is: " << boxVolume(10);

    // specify length and width; default height
    std::cout << "\n\nThe volume of a box with length 10,\n" << "width 5 and height 1 is: " << boxVolume(10, 5);

    // specify all arguments
    std::cout << "\n\nThe volume of a box with length 10,\n" << "width 5 and height 2 is: " << boxVolume(10, 5, 2) << std::endl;

    return 0;/*ww  w  .ja  va  2s . com*/
}
// function boxVolume calculates the volume of a box
int boxVolume(int length, int width, int height) {
    return length * width * height;
}

Result


Related Tutorials