Skip to main content

Most Visited Blog

How to Install minGW C++ compiler for Windows - Cyborg Coding

Formal and Actual Parameters with examples in C++ - Cyborg Coding

In C++, when a function is called, the values passed to the function are called "actual parameters" and the variables in the function definition that receive these values are called "formal parameters".

For example, consider the following function definition:

void myFunction(int x, double y) { 

    // function code 

}

In this example, x and y are the formal parameters of the function. When the function is called, it takes actual parameters that are assigned to the formal parameters in the function definition. For example:

myFunction(5, 3.14);

In this example, 5 is passed as the actual parameter for the formal parameter x, and 3.14 is passed as the actual parameter for the formal parameter y.

It's worth noting that the actual parameters passed to a function call do not need to have the same names as the formal parameters in the function definition. It's only important that the types of the actual parameters match the types of the formal parameters.

In summary, formal parameters are the variables in the function definition that receive the values passed to the function, actual parameters are the values passed to the function when it's called. Formal parameters and actual parameters are matched by position, that is, the first actual parameter passed to a function call is assigned to the first formal parameter in the function definition, and so on.

Comments