Question

When compiling in C++ I often end up with error messages dealing with "formal parameters", such as

error C2719: 'b': formal parameter with __declspec(align('16')) won't be aligned

I do understand the error, and the fact that b is a parameter of a function I am defining.

However, what does it mean that a parameter is formal? Can there be informal parameters as well?

I do notice that the term "formal parameter" appears in other languages as well, so I presume it is a more generic term not necessarily specific to C-family of languages? Are informal parameters supported by some subset of languages?


Upon seeing the answers, one final question: Where those names formal parameter and actual parameter origin from? Does it origin from the C standard, or is it an effect of calling it as such in some abstract language calculus?

Was it helpful?

Solution

There are formal and actual parameters:

void foo(int arg); //arg is a formal parameter

int main()
{
    int val = 1;
    foo(val);  //val is an actual parameter
}

From C++ Standard:

1.3.1 formal parameter (parameter)

an object or reference declared as part of a function declaration or definition, or in the catch clause of an exception handler, that acquires a value on entry to the function or handler; an identifier from the comma-separated list bounded by the parentheses immediately following the macro name in a function-like macro definition; or a template-parameter. Parameters are also known as formal arguments or formal parameters.

1.3.10 actual parameter (argument)

an expression in the comma-separated list bounded by the parentheses in a function call expression, a sequence of preprocessing tokens in the comma-separated list bounded by the parentheses in a function-like macro invocation, the operand of throw, or an expression, type-id or template-name in the comma-separated list bounded by the angle brackets in a template instantiation. Also known as an actual argument or actual parameter.

OTHER TIPS

Formal parameters are the parameters known at the function definition. The actual parameters are what you actually (hence the name) pass to the function when you call it.

void foo( int a ); // a is a formal parameter

foo(10); // 10 is the actual parameter

It's a matter of being a little pedantic over terminology, but quite useful: The formal parameters are what you just think of function parameters:

int foo(bool a, float b);

Here a and b are formal parameters. The point is that in the function body, you're referring to those parameters "formally" without actually knowing their value. It is only when you actual evaluate a function call expression that the formal function parameters are bound to the function call arguments:

int result = foo(false, 1.5);

In this call expression, the value false of the first argument is bound to the formal parameter a, and similarly for the second argument.

The distinction between parameters and arguments is maybe more important to language designers and comiler writers, but as an example in C++, it can be very helpful to get your head around this when you're trying to follow the rules for template argument deduction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top