StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Monday, June 16, 2008

C++ Default Arguments

 June 16, 2008     CPP     No comments   

What are default arguments in C++?

  • Default argument is a value given in the function declaration. The compiler automatically inserts this if a value is not provided during the function call.
  • Default arguments are similar to function overloading in the sense that both features allow to use a single function name in different scenarios.
  • The guideline to follow default arguments vs function overloading. When the desired behavior is almost similar it is recommended to use default arguments and when the behavior is different function overloading.
  • Only trailing arguments can be defaulted. Once a particular arguments is made default all the following arguments should also be made default.
  • Default arguments are useful in cases where capability of an existing function is increased by adding a new argument. Existing function calls need not be changed for this.

Demonstrate the usage of default arguments

#include <iostream>

using namespace std;

// Function with default arguments

void myfunc(int a, bool flag = true) {

    if ( flag == true ) {
       // Do something
       cout << "Flag is true. a = " << a << endl;
    }
    else {

       // Do something
       cout << "Flag is false. a = " << a << endl;
    }
}


int main()
{
    myfunc(100);
    myfunc(200, false);
    return 0;
}
OUTPUT:-
Flag is true. a = 100
Flag is false. a = 200
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © StackStalk