Actual Parameter and Formal Parameter in C++28 Aug 2024 | 3 min read 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: C++ Snippet 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: C++ Snippet Actual & Formal Parameters C++ Code Output The result is: 35 Explanation: 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++ |
Cin.get() in C++
In this article, we will discuss the cin.get() function in C++ with its methods and examples. Introduction: The character array can be accessed using the cin.get() function. In the C++ programming language, this fundamental function is used to solicit user feedback. The white space characters are also incorporated...
5 min read
Array of sets in C++
An array is defined as the collection of data items stored in a contiguous manner. An array stores different variables of the same type. It becomes easier to access the variables as they are stored at continuous locations. For example This is an array that contains six elements....
6 min read
Jump Statements in C++
Jump statements are implemented to change the flow of the program when particular conditions are satisfied. It is used within a program to end or continue a loop or to pause the execution of a function. C++ has four jump statements: continue, break, return, and goto. Continue: Instead...
4 min read
PID Controller C++
A PID Controller is a mechanism of control loop feedback which is widely used in engineering applications to maintain the system's behavior. The controller takes the input signal, compares it to the desired setpoint, and generates an output signal to drive the system closer to the...
4 min read
Comparison between Heap and Tree in C++
In this article, you will learn about the comparison between the Heap and Tree with their types and examples. What is Heap? A specialized tree-based data structure that satisfies the heap property is called a heap. The relationship between parent and child nodes is determined by this property,...
10 min read
One time pad algorithm in C++
This C++ application uses the one-time pad cipher technique to encrypt any message. The input is case-insensitive and compatible with all characters. In the decrypted message, white spaces are generated as random characters rather than being disregarded. Example: The C++ program's source code for implementing the one-time pad...
3 min read
Thread pool in C++
A thread pool is a collection of threads, each with a particular task. As a result, various threads do distinct types of jobs. As a result, each thread specializes in different tasks. A thread is responsible for executing a specific set of similar functions, while another...
4 min read
Convert Camel Case String in Snake Case in C++
Are you struggling to make sense of strings with inconsistent formatting in your C++ code? Converting between different styles of string formatting can be a common challenge for programmers, particularly when dealing with Camel Case and Snake Case. Converting a Camel Case string to a Snake Case...
12 min read
What are Macros in C++
? In C++, a macro is defined as the section of the code that contains the macro value that can be replaced. We can define the macro keyword with the help of the #define directive. During the program compilation, the compiler goes to the macros then the...
9 min read
forward_list::unique() in C++
Forward lists are sequence structures that allow constant-time insert and erase processes anywhere in the sequence. Forward lists are implemented as singly linked lists. The ordering is maintained by associating each element with a link to the element in the sequence. forward_list::unique() is a function in...
2 min read
We request you to subscribe our newsletter for upcoming updates.

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