The terms actual and formal parameters in C++ represent the values that are passed and received by a function, respectively. When defining a function, it outlines the quantity, data types, and identifiers of its formal parameters, whereas during a function invocation, the provided actual parameters correspond to these formal parameters. This alignment of actual parameters to formal parameters is known as function call binding or argument passing.
C++ provides various methods for passing arguments, such as call by value, call by reference, and call by address.
- In call by value, a duplicate of the real argument is transferred to the function and held in the related formal argument.
- In call by reference, the memory location of the real argument is transferred to the function, making the formal argument an alias for the real argument, enabling the function to alter its value.
- In call by address, the memory address of the real argument is transferred to the function, turning the formal argument into a pointer to the real argument, allowing the function to indirectly modify its value.
By default, C++ uses call by value as the argument passing mechanism. When an actual argument is passed by value, a temporary duplicate of the argument is generated and stored in the related formal argument. The function works on this duplicate, not the original argument. Consequently, any adjustments made to the formal argument do not impact the actual argument.
When a function requires altering the value of the original argument, call by reference is employed. In this method, the function receives the memory address of the original argument, making the formal argument an alias for the original one. This mechanism enables direct modification of the original argument's value. To implement call by reference, the function definition places the & operator before the formal argument. Call by address is utilized when the function needs to indirectly modify the original argument's value. In this scenario, the function receives the memory address of the original argument, and the formal argument acts as a pointer to the original argument. This approach enables indirect modification of the original argument's value using the pointer. To pass an argument by address, the * operator is positioned before the formal argument in the function definition.
The selection of argument passing mechanism is determined by the requirements of the function. If the function simply requires accessing the argument's value, call by value is appropriate. In cases where the function needs to alter the argument's value, either call by reference or call by address can be employed, based on whether the modification should be immediate or through a pointer.
C++ Code
#include <iostream>
using namespace std;
void print_sum(int a, int b) {
int sum = a + b;
cout << "The sum of " << a << " and " << b << " is: " << sum << endl;
}
int main() {
int num1 = 5, num2 = 10;
cout << "Actual Arguments: " << num1 << ", " << num2 << endl;
print_sum(num1, num2);
return 0;
}
Output
Actual Arguments: 5, 10
The sum of 5 and 10 is: 15
Explanation:
The provided code illustrates the utilization of real and formal parameters in C++. It showcases a function named printsum, which accepts two integer parameters a and b to compute their total. Upon calculation, the function displays the outcome. Within the main function, the printsum function is invoked with two integer inputs num1 and num2 as real parameters.
In C++, the values passed to a function during its invocation are referred to as real arguments, while the variables specified in the function declaration are termed formal arguments. These formal parameters serve as symbolic representations for the concrete values that will be supplied to the function during its call. In the context of this code snippet, a and b serve as the formal parameters within the print_sum function.
In the main function, num1 and num2 are initialized with the values 5 and 10 correspondingly. Upon calling the printsum function within the main function, the values stored in num1 and num2 are transferred to a and b as actual arguments. Subsequently, the printsum function computes the sum of a and b, storing the result in the sum variable.