C++ c+中的函数+;按值调用和按引用调用

C++ c+中的函数+;按值调用和按引用调用,c++,C++,下面的代码是在这两种方法中调用函数的示例 请告诉我按值调用和按引用调用之间的主要区别或含义 1.按值调用。 2.参考电话。 下面的代码演示了按值调用方法 我在评论中详细说明了我的疑虑 #include<iostream> int main(){ void change(int);//why function prototype is before function definition and what is int orig=10;//meaning of argument in

下面的代码是在这两种方法中调用函数的示例 请告诉我按值调用和按引用调用之间的主要区别或含义 1.按值调用。 2.参考电话。 下面的代码演示了按值调用方法

我在评论中详细说明了我的疑虑

#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;
}
#包括
int main(){
void change(int);//为什么函数原型在函数定义之前,什么是
int orig=10;//参数int的含义,它没有定义任何int类型的变量
cout主要区别在于“通过引用传递”传递对象的引用,而不是对象的副本。因此,被调用函数可以访问对象所在的同一内存位置,并在需要时对其进行修改。相反,当传递对象的副本时,对复制的对象进行修改,而不是对原始对象进行修改

您可以在代码中添加以下内容并查看差异:

#include<iostream>
void change(int);
void change_ref(int&);

int main(){

    int orig=10;//meaning of argument int, it did not defined any variable of type int
    cout<<"The original value is: "<<orig<<"\n";
    change_ref(orig);//what is the meaning of this piece of code
    cout<<"Value after change() is over:"<<orig<<"\n";
    return 0;
};

void change_ref(int& orig){
    orig=20;
    cout<<"Value of orig in function change() is:"<<orig<<"\n";
    return;
}
#包括
无效变更(int);
无效更改参考(int&);
int main(){
int orig=10;//参数int的含义,它没有定义任何int类型的变量

cout通过值调用生成参数的副本,并将其放入局部变量中供函数使用,因此如果函数更改局部变量的值,则参数本身不会更改。通过引用调用将参数的引用传递给函数而不是副本,因此如果函数更改参数的值,则论点本身发生了变化

函数原型
void change(int);
告诉编译器有一个名为change的函数,它接受一个
int
类型的参数,并返回
void
(即nothing)。它是按值调用的,因为没有带参数的
&
。稍后在代码中有一行
change(orig);
实际调用带有
orig
类型
int
的参数的函数。由于函数原型是在此函数调用之前声明的,编译器将其识别为函数

看看这个程序的输出:

#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;
}
#包括
使用名称空间std;
int main(){
无效变更(int);
无效更改2(int&);
int x=10;

coutPass by value意味着如果在函数内部更改值,那么它在函数外部没有影响(当函数返回时)。其中as Pass by reference意味着如果在函数中更改值,那么在函数外部也会更改(当函数返回时).

按引用传递或按值传递的区别的一个很好的例子是当数组被传递给函数时。它可以通过指针或引用传递。一个很好的例子是链接

我认为它很好地解释了按值调用和按引用调用之间的主要区别。简言之,参数的更改在被调用函数中,如果按值调用,则(1)不会更改用作函数参数的实际变量,以及(2)如果通过引用调用,将更改用作函数参数的实际变量。请解释一下为什么将int数据类型指定为argunet,因为第2行和第2行中没有任何变量3@aVIRA,这些行是函数声明。在函数声明中,参数名称不是必需的。它们可以在那里,但不能在声明中没有它们,一切都是完整的。