c++中的函数返回引用_C ++中的函数

c++中的函数返回引用

Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check errors etc.

函数用于为程序提供模块化。 使用功能创建应用程序可以更轻松地理解,编辑,检查错误等。

在C ++中使用函数的基本语法 (Basic Syntax for using Functions in C++)

Here is how you define a function in C++,

这是您在C ++中定义函数的方式,

return-type function-name(parameter1, parameter2, ...)
{
    // function-body
}
  • return-type: suggests what the function will return. It can be int, char, some pointer or even a class object. There can be functions which does not return anything, they are mentioned with void.

    return-type:建议函数将返回什么。 它可以是int,char,某些指针甚至是类对象。 可以有一些不返回任何东西的函数,它们用void提到。

  • Function Name: is the name of the function, using the function name it is called.

    函数名称:函数的名称 ,使用被称为的函数名称。

  • Parameters: are variables to hold values of arguments passed while function is called. A function may or may not contain parameter list.

    参数:是用于保存在调用函数时传递的参数值的变量。 一个函数可能包含也可能不包含参数列表。

    // function for adding two values
    void sum(int x, int y)
    {
        int z;
        z = x + y;
        cout << z;
    }
    
    int main()
    {
        int a = 10;
        int b = 20;
        // calling the function with name 'sum'
        sum (a, b);
    }
    Here, a and ab are two variables which are sent as b是被作为arguments to the function 参数发送给函数两个变量sum, and sum ,和x and xy are y是将举行的值parameters which will hold values of 的参数 a and ab to perform the required operation inside the function.b执行功能内所需要的操作。
  • Function body: is the part where the code statements are written.

    功能主体:是编写代码语句的部分。

声明,定义和调用函数 (Declaring, Defining and Calling a Function)

Function declaration, is done to tell the compiler about the existence of the function. Function's return type, its name & parameter list is mentioned. Function body is written in its definition. Lets understand this with help of an example.

完成函数声明是为了告诉编译器该函数的存在。 函数的返回类型,其名称和参数列表被提及。 函数体以其定义形式编写。 让我们借助示例来理解这一点。

#include < iostream>
using namespace std;

//declaring the function
int sum (int x, int y);

int main()
{
    int a = 10;
    int b = 20;
    int c = sum (a, b);    //calling the function
    cout << c;
}

//defining the function
int sum (int x, int y)
{
    return (x + y);
}

Here, initially the function is declared, without body. Then inside main() function it is called, as the function returns sumation of two values, and variable c is there to store the result. Then, at last, function is defined, where the body of function is specified. We can also, declare & define the function together, but then it should be done before it is called.

在此,函数最初声明为 ,没有主体。 然后在main()函数内部将其称为 ,因为该函数返回两个值的和,并且变量c在那里存储结果。 然后,最后定义 function,在其中指定函数主体。 我们也可以一起声明和定义该函数,但是应该在调用它之前完成它。

调用函数 (Calling a Function)

Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them,

函数通过名称来调用。 如果该函数没有参数,则可以使用其名称直接调用它。 但是对于带有参数的函数,我们有两种方法来调用它们,

  1. Call by Value

    按价值致电

  2. Call by Reference

    通过参考电话

按价值致电 (Call by Value)

In this calling technique we pass the values of arguments which are stored or copied into the formal parameters of functions. Hence, the original values are unchanged only the parameters inside function changes.

在这种调用技术中,我们将参数的值传递给存储或复制到函数的形式参数中。 因此,只有函数内部的参数发生变化,原始值才会保持不变。

void calc(int x);

int main()
{
    int x = 10;
    calc(x);
    printf("%d", x);
}

void calc(int x)
{
    x = x + 10 ;
}

10

10

In this case the actual variable x is not changed, because we pass argument by value, hence a copy of x is passed, which is changed, and that copied value is destroyed as the function ends(goes out of scope). So the variable x inside main() still has a value 10.

在这种情况下,实际变量x不会更改,因为我们通过值传递参数,因此传递了x的副本,该副本被更改,并且该副本的值在函数结束时被破坏(超出范围)。 因此main()中的变量x仍然具有值10。

But we can change this program to modify the original x, by making the function calc() return a value, and storing that value in x.

但是我们可以更改该程序以修改原始x ,方法是使函数calc()返回一个值,并将该值存储在x中。

int calc(int x);

int main()
{
    int x = 10;
    x = calc(x);
    printf("%d", x);
}

int calc(int x)
{
    x = x + 10 ;
    return x;
}

20

20

通过参考电话 (Call by Reference)

In this we pass the address of the variable as arguments. In this case the formal parameter can be taken as a reference or a pointer, in both the case they will change the values of the original variable.

在此,我们将变量的地址作为参数传递。 在这种情况下,形式参数可以用作引用或指针,在这两种情况下,它们都会更改原始变量的值。

void calc(int *p);

int main()
{
    int x = 10;
    calc(&x);     // passing address of x as argument
    printf("%d", x);
}

void calc(int *p)
{
    *p = *p + 10;
}

20

20

NOTE: If you do not have a prior knowledge of pointers, do study pointers first.

注意:如果您没有指针的先验知识,请首先研究指针。

翻译自: https://www.studytonight.com/cpp/functions-in-cpp

c++中的函数返回引用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值