Lesson Introduction

In this lesson, we'll dive into function parameters in C++, focusing on default values and references. These concepts help you write more flexible and efficient functions. By the end, you'll know how to set default values for parameters and modify variables directly by passing them by reference.

Default Values in Function Parameters

In C++, you can give function parameters default values. If you don't provide a value when calling the function, the default is used.

Imagine a function that prints a message. Sometimes, you might want to use a default message if none is provided:

Here, displayMessage has a default value of "Hello, World!" for its message parameter. When displayMessage is called without an argument, it prints the default message. With an argument like "Hi there!", it prints that instead.

Default values make functions flexible, reducing the need for additional function variants.

Potential Pitfalls

In C++, parameters with default values must be defined after any parameters without default values. This is important to ensure the function calls are unambiguous.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal