Beginner 12 min

Function Parameters

Learn to pass data into functions using parameters, understand pass-by-value, and create flexible reusable functions

Learn how to pass data into functions using parameters, making your functions flexible and reusable.

A Simple Example

#include <iostream>

// Parameter: x (declared in function signature)
void printDouble(int x) {
    std::cout << x << " doubled is " << (x * 2) << "\n";
}

// Multiple parameters
int add(int x, int y) {
    return x + y;
}

int main() {
    printDouble(5);   // Argument: 5
    printDouble(10);  // Argument: 10
    printDouble(42);  // Argument: 42

    int result{add(5, 10)};
    std::cout << "5 + 10 = " << result << "\n";

    return 0;
}

Breaking It Down

Parameters vs Arguments

  • Parameter: The variable name in the function definition (e.g., void printDouble(int x))
  • Argument: The actual value you pass when calling the function (e.g., printDouble(5))
  • Think of parameters as placeholders, arguments as the actual data
  • Remember: Parameters are defined once, arguments are provided each time you call

Pass-by-Value

  • What it does: Creates a copy of the argument and passes it to the function
  • Safety feature: The function cannot modify the original variable
  • The copy lives only inside the function (local scope)
  • Remember: Changes to parameters inside the function don't affect the originals

Multiple Parameters

  • Functions can have multiple parameters separated by commas
  • Arguments must be provided in the same order as parameters
  • Each parameter needs its own type declaration
  • Example: int add(int x, int y) requires two int arguments

Type Matching

  • Arguments must match parameter types (or be convertible)
  • C++ may perform implicit type conversion (e.g., int to double)
  • Mismatched types cause compiler errors or unexpected behavior
  • Remember: Be explicit with types to avoid surprises

Why This Matters

  • Functions without parameters can only do one thing in one way.
  • Parameters transform your functions from rigid, single-purpose code into flexible tools that can work with different data each time they're called.
  • This is what makes functions truly reusable across your entire program.

Critical Insight

Pass-by-value means the function gets a *copy* of your data. This is actually a safety feature! The function can't accidentally modify your original variables:

#include <iostream>

void tryToDouble(int number) {
    number = number * 2;  // Only changes the copy!
    std::cout << "Inside function: " << number << "\n";
}

int main() {
    int value{10};
    tryToDouble(value);
    std::cout << "After function: " << value << "\n";  // Still 10!

    return 0;
}

This isolation prevents bugs and makes functions more predictable. Want to modify the original? That's what references are for (you'll learn that later)!

Best Practices

Use descriptive parameter names: Choose names that clearly indicate what the parameter represents (e.g., age, price, userName).

Keep parameter count reasonable: Functions with many parameters become hard to use. Consider grouping related data into a struct.

Order parameters logically: Put required parameters first, optional ones last. Group related parameters together.

Match types explicitly: Don't rely on implicit conversions. If your function expects an int, pass an int.

Common Mistakes

Argument Type Mismatch: Passing the wrong type can cause errors or unexpected behavior. If a function expects double, passing int may work but lose precision.

Wrong Number of Arguments: Must match parameter count exactly. Calling multiply(5) when it expects two parameters causes a compiler error.

Thinking Pass-by-Value Modifies Originals: The function gets a copy, not the original. Changes inside the function don't affect the caller's variables.

Parameter Order Confusion: Arguments are matched by position, not name. Calling divide(10, 2) is different from divide(2, 10).

Debug Challenge

This function call is missing an argument. Click the highlighted line to fix it:

1 #include <iostream>
2
3 int multiply(int x, int y) {
4 return x * y;
5 }
6
7 int main() {
8 int result{multiply(5)};
9 std::cout << "Result: " << result << "\n";
10 return 0;
11 }

Quick Quiz

  1. What's the difference between a parameter and an argument?
Parameters are for numbers, arguments are for strings
They're the same thing
Parameters are optional, arguments are required
Parameters are in the function definition, arguments are passed when calling
  1. What happens to the original variable when you pass it by value?
It remains unchanged
It's deleted after the function call
It becomes read-only for the rest of the program
It gets modified by the function
  1. What happens if you call a function with the wrong number of arguments?
Runtime error when the function executes
The function uses default values
Compiler error - must match parameter count exactly
It works fine, extra arguments are ignored

Step Through the Code

Walk through the code step by step. Watch how variables change and see the program output at each line.

Lesson Progress

  • Fix This Code
  • Quick Quiz
  • Practice Playground - run once