In the realm of programming, parameters play a crucial role in transferring data from one module to another. C++ offers a system for transferring data between functions using real and formal parameters. This guide delves into the ideas of real and formal parameters in C++, covering their structure, application, along with the primary function in C++ and its significance in running programs. Real parameters denote the values sent to a function during its invocation, embodying the specific data transmitted to the function. Take, for instance, the code snippet below:
C++ Snippet
int add(int a, int b) {
return a + b;
}
int main() {
int x = 5;
int y = 7;
int result = add(x, y);
cout << result << endl;
return 0;
}
In the provided code snippet, the function add accepts a pair of arguments, denoted as a and b, serving as formal parameters. The values x and y represent the respective actual parameters supplied to the function during its invocation. Upon execution, a is assigned the value of x, while b is assigned the value of y.
In contrast, formal parameters are variables specified in the function definition to store the values of the actual parameters passed to the function. In the given instance, a and b serve as the formal parameters declared within the add function definition. Upon calling the function, the values of the actual parameters get assigned to these formal parameters.
It's essential to understand that formal parameters and actual parameters are distinct entities in C++, with modifications to formal parameters having no impact on actual parameters due to the language's pass-by-value approach. This method involves duplicating the values of actual parameters into the formal parameters, ensuring that alterations made to the formal parameters within a function remain isolated from the actual parameters. C++ offers various parameter-passing techniques, such as pass-by-reference and pass-by-pointer, in addition to the default pass-by-value. In the given scenario, the function add receives a and b by value, maintaining their separate identities despite potentially existing in the same file.
Pass-by-reference is an alternative technique for passing arguments in C++, which includes passing the memory location of the real parameters to the function. Here, the formal parameters are defined as references to the real parameters, indicating that they point to the identical memory address as the real parameters. This enables the function to make changes to the values of the real parameters. As an illustration:
C++ Snippet
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5;
int y = 7;
swap(x, y);
cout << x << " " << y << endl;
return 0;
}
Actual & Formal Parameters C++ Code
#include <iostream>
// Function with formal parameters
int multiply(int x, int y) {
return x * y;
}
int main() {
// Actual parameters
int a = 5;
int b = 7;
// Call to the function with actual parameters
int result = multiply(a, b);
std::cout << "The result is: " << result << std::endl;
return 0;
}
Output
The result is: 35
Explanation:
In this instance, the function named multiply accepts two arguments, x and y, specified as integers. Within the primary method, two variables, a and b, are defined and set with values 5 and 7 correspondingly. These variables serve as the concrete arguments that are supplied to the function multiply during its invocation.
Upon execution of the function, the values from the arguments a and b are transferred to the corresponding parameters x and y. Following this assignment, the function proceeds to carry out the multiplication operation on x and y, ultimately providing the outcome as the return value.
The primary function proceeds to display the outcome of the function invocation, resulting in the value 35. This showcases the mechanism of transferring data between functions in C++ using actual and formal parameters.