codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

19 / 87 topics
18Function Basics & User-defined Types19Function Parameters20Inline Functions21Function Overloading22Scope23Recursion24Lambda Expressions
Tutorials/C++ Programming/Function Parameters
⚡C++ Programming

Function Parameters

Updated 2026-05-12
30 min read

Function Parameters

In the previous tutorial, we explored the basics of functions and how to define and use user-defined function types in C++. Now, let's dive deeper into how we can pass parameters to these functions. Understanding different ways to pass parameters is crucial for writing efficient and effective C++ programs.

Introduction

Function parameters allow you to pass data from one part of your program to another, enabling functions to operate on that data. In this tutorial, we'll cover four main types of function parameters: passing by value, passing by reference, default parameters, and const parameters. Each method has its own use cases and implications for performance and functionality.

Passing Parameters by Value

Passing parameters by value means that a copy of the actual parameter is made and passed to the function. This ensures that any changes made to the parameter inside the function do not affect the original data outside the function.

Example 1: Basic Pass by Value

C++
1#include <iostream>
2using namespace std;
3
4void increment(int x) {
5 x++;
6}
7
8int main() {
9 int a = 5;
10 cout << "Before increment: " << a << endl;
11 increment(a);
12 cout << "After increment: " << a << endl;
13 return 0;
14}
Output
Before increment: 5
After increment: 5

In this example, the increment function receives a copy of the variable a. Therefore, any changes to x inside the function do not affect the original variable a.

When to Use Pass by Value

  • When the function does not need to modify the original data.
  • For small data types like integers and floating-point numbers where copying is inexpensive.

Passing Parameters by Reference

Passing parameters by reference allows the function to access the actual memory location of the variable, rather than a copy. This means that any changes made to the parameter inside the function will affect the original data.

Example 2: Basic Pass by Reference

C++
1#include <iostream>
2using namespace std;
3
4void increment(int &x) {
5 x++;
6}
7
8int main() {
9 int a = 5;
10 cout << "Before increment: " << a << endl;
11 increment(a);
12 cout << "After increment: " << a << endl;
13 return 0;
14}
Output
Before increment: 5
After increment: 6

In this example, the increment function receives a reference to the variable a. Therefore, any changes to x inside the function affect the original variable a.

When to Use Pass by Reference

  • When the function needs to modify the original data.
  • For large data types like objects or arrays where copying is expensive.

Default Parameters

Default parameters allow you to specify default values for function parameters. If a value for that parameter is not provided when the function is called, the default value is used.

Example 3: Using Default Parameters

C++
1#include <iostream>
2using namespace std;
3
4void printMessage(string message = "Hello, World!") {
5 cout << message << endl;
6}
7
8int main() {
9 printMessage(); // Uses default parameter
10 printMessage("Goodbye!"); // Overrides default parameter
11 return 0;
12}
Output
Hello, World!
Goodbye!

In this example, the printMessage function has a default parameter of "Hello, World!". When called without an argument, it uses the default value. When called with an argument, it overrides the default value.

Important Notes

  • Default parameters must be specified from right to left. If you have multiple default parameters, all trailing parameters must have default values.
  • Only one function can have a given set of parameter types and defaults.

const Parameters

Using const with parameters ensures that the function does not modify the passed data. This is particularly useful when passing by reference to prevent accidental modifications.

Example 4: Using const Parameters

C++
1#include <iostream>
2using namespace std;
3
4void printValue(const int &x) {
5 cout << x << endl;
6}
7
8int main() {
9 int a = 5;
10 printValue(a);
11 return 0;
12}
Output

In this example, we have a function modifyValue that modifies the passed integer by reference. The printValue function prints the value of an integer using a const reference to ensure it does not modify the data. The addNumbers function demonstrates default parameters, allowing it to be called with or without arguments.

Summary

ConceptDescription
Pass by ValueCopies the actual parameter to the function; changes do not affect original.
Pass by ReferenceAllows access to the actual memory location of the variable; changes affect original.
Default ParametersSpecifies default values for parameters; allows function calls with fewer arguments.
const ParametersEnsures that the function does not modify the passed data; useful for safety and clarity.

What's Next?

In the next tutorial, we'll explore Inline Functions. Inline functions are a way to suggest to the compiler that it should try to insert the body of the function at each point where the function is called. This can improve performance by reducing the overhead of function calls.

Stay tuned for more insights into C++ programming!


PreviousFunction Basics & User-defined TypesNext Inline Functions

Recommended Gear

Function Basics & User-defined TypesInline Functions