I wish to know what is meant by "default" in programming.

Recommended Answers

All 6 Replies

Could you put your question in context by giving us an example...

switch( var ) {
  case 1:
    // ;
    break;
  default:
    std::cout << "Default." << std::endl;
    break;
}

Any case except the integer 1 will output "Default.", as it is the default statement.

I cannot think of a different context for this question, if it's something else you'll need to be more specific.

switch( var ) {
  case 1:
    // ;
    break;
  default:
    std::cout << "Default." << std::endl;
    break;
}

Any case except the integer 1 will output "Default.", as it is the default statement.

I cannot think of a different context for this question, if it's something else you'll need to be more specific.

A "default" is just a value or action that happens automatically if some other action or value is not specified in its place.

The switch "default case" is just one example. It is probably the only one example that actually uses the keyword "default". It's the case that executes if no other case matches the value of variable used as a selector. But there are other types of "defaults" in different contexts.

Default function parameters:

int someFunc(int aNumber, int defaultParam = 10) {
  /* ... */
}

In this example, you can call the function as either someFunc(int) or as someFunc(int, int). This is permissible because the second argument/parameter has a default value of 10 if no second argument is provided to the function call.

int multiply(int aNumber, int multiplier = 10);

int main() {
  int product1(0), product2(0);

  product1 = multiply(1);      //calls multiply() with default second argument of 10
  product2 = multiply(2, 20);  //calls multiply(), does not use default value of second argument

  return 0;
}

int multiply(int aNumber, int multiplier) {
  return (aNumber * multiplier);
}

In that sample, product 1 will be 10, and product2 will be 40.

Default class constructors:

class sampleClass {
 public:
   sampleClass();   //"default" constructor
   sampleClass(int);//overloaded or "specified" constructor
};

The default constructor allows declarations such as this:

someClass defaultSomeClassObj;        //declares a "default" "someClass" object
someClass specifiedSomeClassObj(2);   //declares a "specified" "someClass" object

A "default" is just a value or action that happens automatically if some other action or value is not specified in its place.

The switch "default case" is just one example. It is probably the only one example that actually uses the keyword "default". It's the case that executes if no other case matches the value of variable used as a selector. But there are other types of "defaults" in different contexts.

Default function parameters:

int someFunc(int aNumber, int defaultParam = 10) {
  /* ... */
}

In this example, you can call the function as either someFunc(int) or as someFunc(int, int). This is permissible because the second argument/parameter has a default value of 10 if no second argument is provided to the function call.

int multiply(int aNumber, int multiplier = 10);

int main() {
  int product1(0), product2(0);

  product1 = multiply(1);      //calls multiply() with default second argument of 10
  product2 = multiply(2, 20);  //calls multiply(), does not use default value of second argument

  return 0;
}

int multiply(int aNumber, int multiplier) {
  return (aNumber * multiplier);
}

In that sample, product 1 will be 10, and product2 will be 40.

Default class constructors:

class sampleClass {
 public:
   sampleClass();   //"default" constructor
   sampleClass(int);//overloaded or "specified" constructor
};

The default constructor allows declarations such as this:

someClass defaultSomeClassObj;        //declares a "default" "someClass" object
someClass specifiedSomeClassObj(2);   //declares a "specified" "someClass" object

Nice list here's another one to add

What are the default values of the fundamental types?

Nice list here's another one to add

What are the default values of the fundamental types?

Technically, there is no such thing. That being said, some compilers do implement defaults for certain types.

That's why it's considered good practice to make a point of initializing them yourself. There is no guarantee that your compiler will automatically set your uninitialized int to 0, for example. It is far better to explicitly initialize a variable than it is to rely on a compiler feature/quirk.

>What are the default values of the fundamental types?
That's a fun little can of worms. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.