Actual Argument and Formal Argument in C++Actual and formal arguments in C++ refer to the values passed and received by a function, respectively. A function definition specifies the number, types, and names of its formal arguments, while a function call provides the corresponding actual arguments. The process of matching actual arguments with formal arguments is called function call binding or argument passing. C++ supports several argument passing mechanisms, including call by value, call by reference, and call by address. In call by value, a copy of the actual argument is passed to the function and stored in the corresponding formal argument. In call by reference, the address of the actual argument is passed to the function and the formal argument becomes an alias for the actual argument, allowing the function to modify its value. In call by address, the address of the actual argument is passed to the function and the formal argument becomes a pointer to the actual argument, allowing the function to modify its value indirectly. Call by value is the default argument passing mechanism in C++. When an actual argument is passed by value, a temporary copy of the argument is created and stored in the corresponding formal argument. The function operates on the copy, not the original argument. This means that any changes made to the formal argument are not reflected in the actual argument. Call by reference is used when the function needs to modify the value of the actual argument. In call by reference, the address of the actual argument is passed to the function and the formal argument becomes an alias for the actual argument. This allows the function to directly modify the value of the actual argument. To pass an argument by reference, the & operator is placed before the formal argument in the function definition. Call by address is used when the function needs to modify the value of the actual argument indirectly. In call by address, the address of the actual argument is passed to the function and the formal argument becomes a pointer to the actual argument. This allows the function to modify the value of the actual argument indirectly, using the pointer. To pass an argument by address, the * operator is placed before the formal argument in the function definition. The choice of argument passing mechanism depends on the needs of the function. If the function only needs to read the value of the argument, call by value is sufficient. If the function needs to modify the value of the argument, either call by reference or call by address can be used, depending on whether the modification should be direct or indirect. C++ Code Output Actual Arguments: 5, 10 The sum of 5 and 10 is: 15 Explanation: The code demonstrates the use of actual and formal arguments in C++. It contains a function print_sum that takes two integer arguments a and b and calculates their sum. The function then prints the result. The main method calls the print_sum function and passes two integer values num1 and num2 as actual arguments. In C++, when a function is called, the values passed to the function are known as actual arguments. The parameters specified in the function definition are called formal arguments. The formal arguments act as placeholders for the actual values that will be passed to the function when it is called. In this code, a and b are the formal arguments in the print_sum function. In the main method, num1 and num2 are assigned the values 5 and 10, respectively. When the print_sum function is called in the main method, the values of num1 and num2 are passed to a and b as actual arguments. The print_sum function calculates the sum of a and b, and stores the result in the sum variable. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India