Default function Parameter

COS1511 - Introduction To Programming I · Learning C++

Default Function Parameter

A default function parameter allows you to define a value for a function parameter that will be used if no argument is provided when the function is called. This can simplify function calls and make your code cleaner.

Defining Default Parameters

To define a default parameter, you specify the default value in the function declaration. This value is used if the caller does not provide a corresponding argument. Here is the syntax:

return_type function_name(parameter_type parameter_name = default_value) {

For example, consider a function that calculates the area of a rectangle. You may want to provide a default value for the width if it is not specified:

double calculateArea(double length, double width = 5.0) {

In this case, if you call calculateArea(10.0), the width will automatically be set to 5.0.

Example of a Default Parameter

Let’s look at a complete example:

#include <iostream> using namespace std; double calculateArea(double length, double width = 5.0) { return length * width; } int main() { cout << "Area with default width: " << calculateArea(10.0) << endl; cout << "Area with specified width: " << calculateArea(10.0, 2.0) << endl; return 0; }

In this code:

  • The function calculateArea takes two parameters: length and width.
  • width has a default value of 5.0.
  • In main, the first call to calculateArea(10.0) uses the default width of 5.0.
  • The second call, calculateArea(10.0, 2.0), specifies a width of 2.0.

The output will be:

Area with default width: 50 Area with specified width: 20

Multiple Default Parameters

You can also define multiple default parameters in a function. However, when using default parameters, they must be defined from right to left. This means that if a parameter has a default value, all parameters to its right must also have default values.

For example:

void displayInfo(string name, int age = 18, string city = "Cape Town") { cout << "Name: " << name << ", Age: " << age << ", City: " << city << endl; }

In this function, you can call displayInfo("John") and it will use the default values for age and city.

Example with Multiple Default Parameters

Here is a complete example:

#include <iostream> using namespace std; void displayInfo(string name, int age = 18, string city = "Cape Town") { cout << "Name: " << name << ", Age: " << age << ", City: " << city << endl; } int main() { displayInfo("John"); displayInfo("Alice", 25); displayInfo("Bob", 30, "Durban"); return 0; }

The output will be:

Name: John, Age: 18, City: Cape Town Name: Alice, Age: 25, City: Cape Town Name: Bob, Age: 30, City: Durban

Watch Out for Common Mistakes

Watch out: Do not mix default parameters with non-default parameters in a function declaration. For example, this is incorrect:

void example(int a = 10, int b);

In this case, b cannot have a default value because it is placed after a, which has a default value.

Benefits of Default Parameters

Using default parameters can lead to cleaner and more readable code. It reduces the number of overloaded functions you need to write and makes function calls easier. You can call a function with fewer arguments without losing functionality.

Limitations of Default Parameters

While default parameters are useful, they also have some limitations:

  • Default parameters can only be specified in the function declaration, not in the definition.
  • All default parameters must be defined from right to left.
  • Using too many default parameters can make the function signature long and harder to read.

Conclusion

Default function parameters provide a convenient way to assign default values to function parameters. They simplify function calls and improve code readability. When using default parameters, remember the rules about their placement and the limitations.

Check your understanding

  • What is a default parameter in C++?
  • How do you define a function with multiple default parameters?
  • What are some common mistakes to avoid when using default parameters?
  • Why might you choose to use default parameters in your functions?