Actual Parameter and Formal Parameter in C++
In the world of programming, parameters are an integral part of the method of passing data from one component to another. C++ provides a mechanism for passing data between functions through the use of actual and formal parameters. In this article, we will explore the concepts of actual and formal parameters in C++, including their syntax and usage, as well as the main method in C++ and its importance in executing programs. Actual parameters are values that are passed to a function when it is called. They represent the actual data that is being passed to the function. For example, consider the following code:
In the code above, the function add takes two parameters, a and b, which are referred to as formal parameters. The values x and y are the actual parameters that are being passed to the function when it is called. When the function is executed, a is assigned the value of x, and b is assigned the value of y.
Formal parameters, on the other hand, are variables that are declared in the function definition and are used to hold the actual parameters passed to the function. In the example above, a and b are the formal parameters that are declared in the function definition of add. When the function is called, the values of the actual parameters are assigned to the formal parameters.
It is important to note that actual parameters and formal parameters are two different entities, and changes made to formal parameters do not affect the actual parameters. This is because C++ uses pass-by-value semantics, meaning that the values of actual parameters are copied into the formal parameters. This means that any changes made to the formal parameters within the function will not affect the actual parameters. There are several different ways to pass parameters to a function in C++, including pass-by-value, pass-by-reference, and pass-by-pointer. Pass-by-value is the default method of passing parameters in C++, and it involves copying the values of the actual parameters into the formal parameters. In the example above, a and b are passed by value to the function add. They might be written in the same file but are dufferent in nature.
Pass-by-reference is another method of passing parameters in C++, and it involves passing the memory address of the actual parameters to the function. In this case, the formal parameters are declared as references to the actual parameters, meaning that they refer to the same memory location as the actual parameters. This allows the function to modify the values of the actual parameters. For example:
Actual & Formal Parameters C++ Code
The result is: 35
In this example, the function multiply takes two formal parameters, x and y, which are declared as integers. In the main method, two variables a and b are declared and initialized with values 5 and 7 respectively. These variables are the actual parameters that are passed to the function multiply when it is called.
When the function is executed, the values of the actual parameters a and b are assigned to the formal parameters x and y respectively. The function then performs the multiplication of x and y and returns the result.
The main method then prints the result of the function call, which is 35. This demonstrates how actual parameters and formal parameters can be used to pass data between functions in C++.
Next TopicAdding Two Strings in C++