Home / Programming / Programming Languages / C++ / Introduction to default parameters in C++

Introduction to default parameters in C++

Print Friendly, PDF & Email

What is a default parameter? When do you have to use default parameters? How to define a function having default parameters? These questions will be discussed in this post. Let’s get started.

What are the default parameters?

A default parameter or argument is a value given in a function declaration so that its callers can use without feeding a specific value if they don’t want to or miss to provide a value for this argument.

In other programming languages such as Java or Python, they are called optional parameters. As the name suggests, they can be omitted. To ease management optional arguments in compilers and a conventional manner to allow programmers to define their parameters of interest, they must follow the required ones. The optional parameters are considered trailing arguments. Obviously, default arguments are used in place of the missing arguments in a function call.

Let’s give an example.

string getQueryStringForUser(string username = 'administrator', bool isAdmin = true);

// get the query string for the user which the username 
// is 'tung' and he isn't an administrator
// calls getQueryStringForUser('tung', false)
getQueryStringForUser('tung', true);  

// get the query string for the user which the username 
// is 'hue' and she isn't an administrator
// calls getQueryStringForUser('hue', false)
getQueryStringForUser('hue');

// get the query string for the user which the username 
// is 'administrator' and this user is an administrator
// calls getQueryStringForUser('administrator', true)
getQueryStringForUser();
Default parameters in C++

Default parameters in C++

Since now, the following pairs of words are being interchangeably used in this post as well as other posts in our website.

Working of default arguments

Found in the writing about C++ programming default argument, the image getting from this post explicitly explains how default arguments are detected in term of a compiler.

Working of default arguments in C++

Working of default arguments in C++

How to define and employ default parameters

In theory, a function can have multiple default arguments. Nevertheless, it is important to remember that the other arguments following the first optional parameter must be optional too.

A single default argument

If our function has a default argument, it has to be the last one in the list of arguments. In the function declaration of arguments, the default one must give a specific value. Let’s get back the introductory example.

string getQueryStringForUser(string username, bool isAdmin = false);

isAdmin is the unique default value.

Multiple default arguments

Let’s consider a complete snippet of the introductory example. The purpose is to build a query string based on input arguments which are username, the number of records on each request. If the username is administrator, he can see more records.

#include <iostream>
#include <string>

using namespace std;

string getQueryStringForUser(string username, int limit = 10, 
                             bool isAdmin = false) {
    string query = "select * from model";

    if (!isAdmin) {
        query += " where owner = '" + username + "'"; 
    }
    query += " limit " + to_string(limit) + ";";
    return query;
}

int main() {
    cout << getQueryStringForUser("tung", 100, true) <<endl;
    cout << getQueryStringForUser("hue", 20) <<endl;
    cout << getQueryStringForUser("administrator") <<endl;
}

This example defines two default arguments, limit and isAdmin. The output shows below.

select * from model limit 100;
select * from model where owner = 'hue' limit 20;
select * from model where owner = 'administrator' limit 10;

That’s it!!!

Summary

I have presented a short introduction to default arguments in C++. The usage of default arguments could be practical and flexible.

Happy coding!

References

  1. C++ Programming Default Arguments (Parameters) accessed 06/06/2020

About Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

code

This site uses Akismet to reduce spam. Learn how your comment data is processed.