Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a formal parameter?

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?

like image 236
CygnusX1 Avatar asked Sep 18 '13 10:09

CygnusX1


People also ask

What is meant by a formal parameter?

formal parameter — the identifier used in a method to stand for the value that is passed into the method by a caller. For example, amount is a formal parameter of processDeposit.

What is formal and informal parameter?

Formal Parameters. When a function is called, the values (expressions) that are passed in the function call are called the arguments or actual parameters. The parameter used in function definition statement which contain data type on its time of declaration is called formal parameter.

What are formal parameters in C language?

Formal Parameter : A variable and its type as they appear in the prototype of the function or method. Actual Parameter : The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment.

What is formal parameter and real parameter?

The parameters used in the procedure/function definition are called the formal parameters. The parameters used in the procedure/function call are called the actual parameters.


3 Answers

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.

like image 61
cpp Avatar answered Oct 14 '22 10:10

cpp


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 
like image 45
SingerOfTheFall Avatar answered Oct 14 '22 10:10

SingerOfTheFall


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.

like image 44
Kerrek SB Avatar answered Oct 14 '22 10:10

Kerrek SB