Ugrás a tartalomhoz

Szerkesztő:LinguisticMystic/cpp/Parameters

A Wikiszótárból, a nyitott szótárból

🔧 Functions in C++: Passing Input Data

[szerkesztés]

In the realm of C++ programming, functions are fundamental for building modular, maintainable, and efficient code. Think of a function as a black box: you give it input, it does something, and possibly returns a result.

But how do we provide input to functions? That’s where parameters and arguments come into play. Understanding the different data-passing techniques is critical for writing optimized and correct programs.



📌 Parameters vs Arguments

[szerkesztés]

Before diving into the techniques, it’s essential to distinguish:

  • Parameters: Variables in the function declaration (or definition). These are placeholders.
  • Arguments: Actual values or variables passed to the function when it is called.
void printValue(int num); // 'num' is a parameter
printValue(42);           // 42 is an argument

✅ Three Ways to Pass Arguments

[szerkesztés]

In C++, arguments can be passed to functions in three primary ways:

  1. Pass by Value
  2. Pass by Reference
  3. Pass by Pointer

Each has trade-offs in terms of performance, memory usage, and side effects.



1️⃣ Passing by Value

[szerkesztés]

Concept:

[szerkesztés]
  • A copy of the argument is made and passed to the function.
  • Changes made inside the function do not affect the original variable.

Syntax:

[szerkesztés]
void modify(int x) {
    x = 10;
}

Example:

[szerkesztés]
#include <iostream>

void modify(int num) {
    num = 10;             // Only modifies the local copy
    std::cout << "Inside: " << num << std::endl;
}

int main() {
    int x = 5;
    modify(x);
    std::cout << "Outside: " << x << std::endl;
    return 0;
}

Output:

[szerkesztés]
Inside: 10
Outside: 5

Use case:

[szerkesztés]
  • When you don’t need to modify the original value.
  • Best for small, primitive types (e.g., int, char).



2️⃣ Passing by Reference

[szerkesztés]

Concept:

[szerkesztés]
  • A reference (alias) to the original variable is passed.
  • The function directly modifies the original variable.

Syntax:

[szerkesztés]
void modify(int &x) {
    x = 10;
}

Example:

[szerkesztés]
#include <iostream>

void modify(int &num) {
    num = 10;             // Modifies the original
}

int main() {
    int x = 5;
    modify(x);
    std::cout << x << std::endl;
    return 0;
}

Output:

[szerkesztés]
10

Use case:

[szerkesztés]
  • When you want to change the original variable.
  • When passing large objects (e.g., std::string, custom classes) to avoid copying.



3️⃣ Passing by Pointer

[szerkesztés]

Concept:

[szerkesztés]
  • The function receives a memory address of the variable.
  • The value can be changed using dereferencing.

Syntax:

[szerkesztés]
void modify(int *ptr) {
    *ptr = 10;
}

Example:

[szerkesztés]
#include <iostream>

void modify(int *num) {
    *num = 10;
}

int main() {
    int x = 5;
    modify(&x);           // Pass the address of x
    std::cout << x << std::endl;
    return 0;
}

Output:

[szerkesztés]
10

Use case:

[szerkesztés]
  • When working with dynamic memory, arrays, or when null-checking is needed.
  • Offers more control, but adds complexity and risk (e.g., null pointers, memory leaks).



💡 Summary Table

[szerkesztés]
Method Copies Data? Can Modify Original? Use Case
Pass by Value ✅ Yes ❌ No Safe, for small/immutable types
Pass by Reference ❌ No ✅ Yes Efficient, for large/mutable data
Pass by Pointer ❌ No ✅ Yes (via *) For optional values, dynamic data



🔐 Best Practices

[szerkesztés]
  • Use const & when passing large data without modification:

    void printLargeObject(const MyClass &obj);
    
  • Use references to modify original data safely.

  • ⚠️ Use pointers when:

    • You deal with dynamic memory,
    • You need to pass null to indicate “no value”,
    • Or you work with arrays.
  • Avoid unnecessary copying (especially of large containers or classes) when performance matters.

  • 📝 Always document your functions:

    // Modifies the original value of x
    void updateValue(int &x);
    



🧠 Conclusion

[szerkesztés]

Understanding how to pass input data to functions is essential for writing safe, performant, and clear C++ code. Each method has its purpose:

  • Pass by value is simple and safe.
  • Pass by reference is efficient and powerful.
  • Pass by pointer is flexible and low-level.

By mastering these techniques, you can design functions that are both efficient and predictable, and optimize your code for both readability and performance.