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>
#include <string>
// Parameter: name (declared in function signature)
void greet(std::string name) {
std::cout << "Hello, " << name << "!" << "\n";
}
// Multiple parameters
int add(int x, int y) {
return x + y;
}
int main() {
greet("Alice"); // Argument: "Alice"
greet("Bob"); // Argument: "Bob"
greet("Carol"); // Argument: "Carol"
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 greet(std::string name)) -
Argument: The actual value you pass when calling the function (e.g.,
greet("Alice")) - 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:
Quick Quiz
- What's the difference between a parameter and an argument?
- What happens to the original variable when you pass it by value?
- What happens if you call a function with the wrong number of arguments?
Practice Playground
Time to try out what you just learned! Play with the example code below, experiment by making changes and running the code to deepen your understanding.
Output:
Error:
Lesson Progress
- Fix This Code
- Quick Quiz
- Practice Playground - run once