What is a formal parameter?

前端 未结 3 1282
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 07:37

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

error C2719: \'b\': formal parameter with __declspec(ali         


        
3条回答
  •  一个人的身影
    2020-12-08 08:11

    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.

提交回复
热议问题