C++ from Scratch to Advance / Functions in C++ / Function Parameters and its Types
Home /C++ from Scratch to Advance /Functions in C++ /Function Parameters and its Types

Function Parameters and its Types

When you are discovering C++ step by step, you eventually need to move data into your functions so they can do real work. In this guide, we explore the different ways to pass information—from making simple copies to sharing direct access to your data—and how to set “backup” values to keep your code running smoothly.

Definitions

Before we dive into the mechanics, let’s define our key concepts simply:

Term Definition / Description
Parameter A variable in a function’s header that acts as a placeholder for incoming data.
Argument The actual value you send to the function when you call it.
Pass by Value Sending a copy of your data to a function. The original remains safe and unchanged.
Pass by Reference Sending the address or “location” of your data. The function works on the actual original data.
Default Argument A “backup” value assigned to a parameter that the program uses if you forget to provide one.
Ampersand (&) The symbol used in C++ to indicate a reference.

1. Pass by Value: The Safety of Copies

In Pass by Value, when you send a variable to a function, C++ creates a duplicate or a “copy” of that value in a new memory location.
The Relatable Example: The Photostat
Imagine you have a master copy of a document. You give a friend a photostat (copy) of it. If your friend draws on their copy or spills coffee on it, your master copy stays perfectly clean. This is exactly how Pass by Value works; any changes the function makes to the variable x or y stay inside that function and do not affect the original variables in main.
When to use it: Use this when you want to perform calculations without any risk of accidentally changing your original data.

Example:

In a swap function without references, the values of A and B remain the same after the function call because only their copies (x and y) were modified.
void swap(int x, int y) { // x=3 and y=4
    int temp = x;
    x = y; // x=4 and y=4
    y = temp; // x=4 and y=3
}

int main() {
    int a = 3, b = 4;
    cout<<"Values of a and b before calling swap function:  "<<endl;
    cout<<"a = "<<a<<",    b= "<<b;
    swap(a, b); // Passing a copy of a and b
    // a and b are still 3 and 4 here 
    cout<<"Values of a and b after calling swap function:  "<<endl;
    cout<<"a = "<<a<<",    b= "<<b;

}

OUTPUT:

Values of a and b before calling swap function:

a = 3,     b=4

Values of a and b after calling swap function:

a = 3,     b=4

2. Pass by Reference: Sharing the Original

Sometimes, you want a function to change your original data—for example, when swapping the values of two variables. For this, we use Pass by Reference. Instead of a copy, you give the function the address of where the data is stored.
The Relatable Example: Google Docs
Think of Pass by Reference like sharing a Live Link to a Google Doc. If you give someone “Editor” access to the link, any change they make is immediately visible on the original document. There is only one document; you are just giving the function a way to find it and work on it directly.
The Syntax: You add the & sign next to the variable type in the function header (e.g., void swap(int &x, int &y)).

Example:

In a swap function with references, the values of A and B will be swapped after the function call because the values at their references (addresses) are modified (&a, &b).
void swap(int &x, int &y) { // x=3 and y=4
       //Original values will now be swapped
       int temp = x;
       x = y; // x=4 and y=4
       y = temp; // x=4 and y=3
 }

int main() {
    int a = 3, b = 4;
    cout<<"Values of a and b before calling swap function:  "<<endl;
    cout<<"a = "<<a<<",    b= "<<b;
    swap(a, b); // Passing memory addresses of a and b
    cout<<"Values of a and b after calling swap function:  "<<endl;
    cout<<"a = "<<a<<",    b= "<<b;

}

OUTPUT:

Values of a and b before calling swap function:

a = 3,     b=4

Values of a and b after calling swap function:

a = 4,     b=3

3. Default Arguments: The Smart Backup System

Default Arguments allow you to assign a value to a parameter directly in the function header. If the user provides a value during the function call, C++ uses that. If the user leaves it blank, C++ uses the “default” you provided.
The “Right-to-Left” Rule
There is one very important rule in C++: you must assign default values starting from the right side of the parameter list and move toward the left.
  • Correct: void sum(int x, int y = 0, int z = 0)
  • Incorrect: void sum(int x = 0, int y, int z)
Why use them? They are incredibly useful for creating “multi-purpose” functions. For example, a single sum function with four parameters could be used to add 2, 3, or 4 numbers simply by setting the last two parameters to a default of 0.

Example:

A single function can be used to add two, three, or four numbers by setting the last two parameters to a default of 0
// Starting from the right side with default values
int sum1(int a, int b, int x = 0, int y = 0) {
    return a + b + x + y; 
}

int main() {
    // Adding 2 numbers (others use default 0)
    cout << sum1(2, 3); // Output: 5 

    // Adding 3 numbers (y uses default 0)
    cout << sum1(2, 3, 4); // Output: 9 
    
    // Adding 4 numbers (all defaults replaced)
    cout << sum1(2, 3, 6, 7); // Output: 18

    return 0;
}

Interactive Elements: Try It Yourself!

  1. Reflection Prompt: Why is Pass by Value the default behavior in C++? (Hint: Think about safety vs. efficiency).
  2. Challenge 1: Write a swap function using the & operator and test it in your compiler. Does it successfully change the variables in main?Challenge 2: Create a function named calculateTotal that takes a price and a taxRate. Set the taxRate to a default value of 0.05.
  3. Challenge 3: Try to define a function with a default value in the middle of the parameter list (e.g., void test(int a, int b = 5, int c)). What error does your compiler show?
  4. Logic Test: If you pass a variable by reference and the function sets it to 0, what will its value be when the program returns to main?

Ready to Master the Code?

For official technical documentation, visit the C++ Reference on Function Parameters.
Happy Coding!
Video Tutorial