DevAcademia lesson/C++ Functions
Multiple Parameters
Topic progressTopic 73 of 109 - 67% complete
~5 min lesson14 learning blocks2 code examples1 checks/interactions
Lesson outline
Parameter Passing Efficiency
Each parameter passed by value creates a copy. For large objects, this can be costly. General guidelines:
- Primitive types (int, char, etc.): pass by value
- Small structs: pass by value or const reference
- Large objects: pass by const reference
- Output parameters: pass by non-const reference (or prefer returning values where appropriate)
| Type | Size | Recommended Passing |
|---|---|---|
| int, float, bool | Typically small (≤8 bytes) | By value |
| Small struct | ≤16 bytes | By value or const reference |
| std::string | Implementation-defined (~24–32 bytes with SSO) | By const reference |
| Large class | >32 bytes | By const reference |
Parameter Order Matters
Organize parameters from most significant to least significant. Place optional parameters at the end:
Alternative Approaches
When functions require many parameters, consider:
- Parameter objects: Bundle related parameters in a struct
- Builder pattern: Chain method calls to configure options
- Named parameters idiom: Use temporary proxy objects for readability
Was this topic clear?