Function Parameters
C++ Function Parameters
Definition: Function parameters act as placeholders or local variables inside a function declaration. They specify the data type and name of the values a function expects to receive when it is executed. When the function is called, the actual values passed to it are known as arguments.
Why: Function parameters are a critical sub-topic in beginner programming tracks because they transform isolated code blocks into dynamic, highly adaptable systems. Instead of writing separate functions for every individual input, parameters allow a single function to process different sets of data cleanly and efficiently.
Syntax for Parameters vs. Arguments
Parameters are defined inside the function's parentheses along with their respective data types. Multiple parameters can be specified by separating them with commas.
void functionName(dataType parameter1, dataType parameter2) {
// The parameters are accessible inside this function block
}
Example: Personalizing Output via Parameters
The program below establishes a greet() function that uses a string parameter to display a personalized greeting on the console screen:
#include <iostream> #include <string> using namespace std; // 'name' is the parameter (placeholder variable) void greet(string name) { cout << "Hello, " << name << endl; } int main() { // "Ramesh" is the argument passed into the function greet("Ramesh"); // Outputs: Hello, Ramesh // You can reuse the function easily with different arguments greet("Suresh"); // Outputs: Hello, Suresh return 0; }
Parameter Passing Techniques
| Passing Method | Syntax Sample | Behavior and Use Case |
|---|---|---|
| Pass by Value | void func(int x) |
The default behavior. C++ makes a brand new copy of the data. Modifying x inside the function does not affect the original variable in main(). |
| Pass by Reference | void func(int &x) |
Passes an alias of the original variable using an ampersand. Modifying x inside the function will directly change the original variable's value. Excellent for avoiding heavy object copying. |
| Default Parameters | void func(int x = 10) |
Assigns an optional fallback value to the parameter. If you call func() without passing an argument, the parameter automatically takes the value 10. |
Key Notes
- Strict Type Checking: C++ is strongly typed. If a function parameter is declared as an
int, passing a text string argument like"Hello"will trigger a compilation breakdown. The argument data types must align with the parameters in exact sequential order. - Position Matters: Arguments map to parameters based entirely on their position in the function call. If you define a function as
void display(string name, int age), you must call it with a string followed by an integer (e.g.,display("Ravi", 20)). Switching the order will result in a structural syntax error. - The Void Return Type: Notice that the example uses
voidas its return type. This simply means the function performs an action (printing text to the screen) but does not compute or send any numerical or logical data value back to themain()program block.
🏋️ Test Yourself With Exercises
Take our quiz on Function Parameters to test your knowledge.
Browse Quizzes »