函数在c ++中通过值调用,并通过引用调用 [英] functions in c++ call by value and call by reference

查看:198
本文介绍了函数在c ++中通过值调用,并通过引用调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码是两个方法中调用函数的示例
请告诉我通过值调用和引用调用之间的主要区别或意义
1.按值调用。
2.通过引用调用。

以下代码说明了按值调用方法。

Following code is an example to call function in both methods Please tell me major difference or meaning between call by value and call by reference 1.Call by value. 2.Call by reference. The following code illustrates call by value method.

p>

I specified my doubts in comments

#include<iostream>
int main(){
void change(int);//why function prototype is before function definition and what is 
int orig=10;//meaning of argument int, it did not defined any variable of type int
cout<<"The original value is: "<<orig<<"\n";
change(orig);//what is the meaning of this piece of code
cout<<"Value after change() is over:"<<orig<<"\n";
return 0;
};
void change(int orig){
orig=20;
cout<<"Value of orig in function change() is:"<<orig<<"\n";
return;
}

在书中我读取函数定义应该在函数原型之前。

In book i read that function definition should before function prototype.

推荐答案

通过值调用创建参数的副本,并将其放在一个局部变量中供函数使用,因此如果函数更改值的局部变量的参数本身没有改变。调用通过引用传递参数的引用到函数而不是副本,因此如果函数更改参数的值,则参数本身改变。

Call by value makes a copy of the argument and puts it in a local variable for use by the function, so if the function changes the value of the local variable the argument itself is not changed. Call by reference passes a reference of the argument to the function rather than a copy, so if the function changes the value of the argument then the argument itself is changed.

函数原型 void change(int); 告诉编译器有一个名为change的函数,它接受 int 并返回 void (即什么都不)。它是通过值调用,因为没有& 与参数。后来在你的代码中你有 change(orig); 这行实际上调用的参数 orig code> int 。因为函数原型在这个函数调用之前被声明,编译器将其识别为一个函数。

The function prototype void change(int); tells the compiler that there is a function named change which takes a single argument of type int and returns void (i.e. nothing). It is call by value since there is no & with the argument. Later in your code you have the line change(orig); which actually calls the function with argument orig of type int. Since the function prototype was declared before this function call the compiler recognizes it as a function.

看看这个程序的输出:

#include<iostream>

using namespace std;

int main(){
  void change(int);
  void change2(int&);
  int x = 10;
  cout<<"The original value of x is: "<< x <<"\n";
  change(x); // call change(), which uses call by value
  cout<<"Value of x after change() is over: "<< x <<"\n";
  change2(x); // call change2(), which uses call by reference
  cout<<"Value of x after change2() is over: "<< x <<"\n";
  return 0;
};

void change(int orig){
    cout<<"Value of orig in function change() at beginning is: "<<orig<<"\n";
    orig=20;
    cout<<"Value of orig in function change() at end is: "<<orig<<"\n";
  return;
}

void change2(int &orig){
    cout<<"Value of orig in function change2() at beginning is: "<<orig<<"\n";
    orig=20;
    cout<<"Value of orig in function change2() at end is: "<<orig<<"\n";
  return;
}

我更改了 int orig main() int x ,希望避免名称混淆, $ c> change2()其中使用通过引用的调用。

I've changed int orig in main() to int x to hopefully avoid name confusion, and I've added change2() which uses call by reference.

这篇关于函数在c ++中通过值调用,并通过引用调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆