什么包含C ++中的函数定义?

时间:2016-05-14 07:06:16

标签: c++ terminology

我认为我从cplusplus.com和MSDN获得的信息与C ++中的“函数定义”完全相同。

MSDN seems to include the parameters

<div class="rate-ex3-cnt" id="<?php echo $productArray[$key]["ID"];?>

            <div id="1" value="1" class="rate-btn-1 rate-btn"></div>
        <div id="2" value="2" class="rate-btn-2 rate-btn"></div>
        <div id="3" value="3" class="rate-btn-3 rate-btn"></div>
        <div id="4" value="4" class="rate-btn-4 rate-btn"></div>
        <div id="5" value="5" class="rate-btn-5 rate-btn"></div>
    </div>

   <script>
        // rating script
        $(function(){ 
            $('.rate-btn').hover(function(){
                $('.rate-btn').removeClass('rate-btn-hover');
                var therate = $(this).attr('id');
                for (var i = therate; i >= 0; i--) {
                    $('.rate-btn-'+i).addClass('rate-btn-hover');
                };
            });

        $('.rate-btn').click(function(){    
            var therate = $(this).attr('id');
            var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
            $('.rate-btn').removeClass('rate-btn-active');
            for (var i = therate; i >= 0; i--) {
                $('.rate-btn-'+i).addClass('rate-btn-active');
            };
            $.ajax({
                type : "POST",
                url : "http://localhost/rating/ajax.php",
                data: dataRate,
                success:function(){}
            });

        });
    });
</script>
  

该函数可以从中的任意数量的位置调用或调用   程序。传递给函数的值是参数,   其类型必须与。中的参数类型兼容   功能定义

cplusplus does not,暗示函数的正文(或者它是返回表达式/值?)是它的定义:

  

重载的函数可能具有相同的定义。例如:

int sum(int a, int b)
{
    return a + b;
}

谷歌搜索“函数定义c ++”获得了很多函数的定义,我不关心。

那么,函数的哪些组件构成了它的定义?

1 个答案:

答案 0 :(得分:5)

让我们直截了当地说明一些术语:

  • 函数声明:也称为函数原型。它是函数签名名称,没有函数体。相反,它后跟一个分号。
  • 函数定义函数声明(不带分号)后跟括号括起来的代码块,称为函数体。< / LI>
  • 函数签名:函数的返回类型和参数类型。这几乎是函数原型,不包括名称
  • 函数体:将以括号括起的代码块的形式执行的实际代码。
  • 函数名称函数原型中不是签名或分号的位。它用于调用函数。

一些例子:

// declaration/prototype
void  // return type
f     // function name
(int) // function parameter list
;     // semicolon
// definition
int g(double) // prototype part of the definition
{ return 42; } // the body, which really "defines" the function
// signature - in between the template's angle brackets < >
std::function<
              int(double)     // this bit is what one would call the signature
                         > h;

签名确定函数(指针)类型,以及在链接器开始将所有内容链接在一起时唯一标识函数的签名+名称。

为什么cplusplus.com说两个函数可以有相同的定义?嗯,这是错的,至少在这个例子中是这样的:

int sum(int a, int b)          { return a+b; }
double sum(double a, double b) { return a+b; }

尽管函数体看起来相同,但它们表达了不同的基本行为:在整数情况下,+表示整数加法,在后一种情况下,它是浮点加法。这是两个不同的(内置)运算符。总而言之,这只是一个令人困惑的例子。