Passing Parameters and Arguments in Function
COS1511 - Introduction To Programming I · Learning C++
Passing Parameters and Arguments in Function
In C++, functions can take inputs called parameters. When you call a function, you provide values for these parameters. These values are known as arguments. Understanding how to pass parameters and arguments is essential for writing effective functions.
Defining Functions with Parameters
When you define a function, you can specify parameters in the function's header. For example, consider the following function that adds two integers:
int add(int a, int b) { return a + b; }In this example, a and b are parameters of the function add. They are placeholders for the values you will provide when you call the function.
Calling Functions with Arguments
To call a function, you provide arguments that match the parameters defined in the function. For example:
int result = add(5, 3);Here, 5 and 3 are the arguments passed to the add function. The function will return 8, which is stored in the variable result.
Remember: The number and type of arguments must match the parameters in the function definition.
Types of Parameters
Parameters can be of various types, such as integers, floats, or even user-defined types. Here are some examples:
void printMessage(string message) { cout << message; }In this function, the parameter message is of type string. You can call this function like this:
printMessage("Hello, UNISA!");Default Parameters
You can set default values for parameters. If an argument is not provided when calling the function, the default value will be used. Here is an example:
void greet(string name = "Student") { cout << "Hello, " << name; }You can call this function with or without an argument:
greet(); // Outputs: Hello, Studentgreet("John"); // Outputs: Hello, JohnTip: Default parameters must be specified from right to left. Once a parameter has a default value, all parameters to its right must also have default values.
Pass by Value vs Pass by Reference
In C++, parameters can be passed by value or by reference. When you pass by value, a copy of the argument is made. Changes to the parameter inside the function do not affect the original argument. For example:
void increment(int x) { x++; }If you call this function:
int num = 5; increment(num); // num remains 5In contrast, when you pass by reference, you pass the address of the argument. Changes to the parameter will affect the original argument. Here is an example:
void increment(int &x) { x++; }Now, if you call the function:
int num = 5; increment(num); // num becomes 6Watch out: Be careful when using pass by reference. Changes made to the parameter will affect the original variable.
Function Overloading
C++ allows you to define multiple functions with the same name but different parameters. This is called function overloading. The compiler determines which function to call based on the number and type of arguments. For example:
int add(int a, int b) { return a + b; }double add(double a, double b) { return a + b; }In this case, there are two functions named add. One adds integers, and the other adds doubles. You can call them like this:
int intSum = add(2, 3); // Calls the first functiondouble doubleSum = add(2.5, 3.5); // Calls the second functionSummary
- Parameters are defined in function headers and act as placeholders for arguments.
- Arguments are the actual values passed to the function when it is called.
- You can set default values for parameters.
- Parameters can be passed by value or by reference.
- C++ supports function overloading based on parameter types and counts.
Check your understanding
- What is the difference between parameters and arguments?
- How do you define a function with default parameters?
- What happens when you pass a parameter by value?
- Can you overload functions with different return types in C++?