![](/img/trans.png)
[英]How to make functions interact with class variables between eachother c++
[英]How do variables between functions and void functions work / interact with each other?
观看此https://youtu.be/_bYFu9mBnr4教程。据我了解,可以调用函数和 void 函数,它们将执行其中的代码。
但是我不明白括号内变量的目的。 如果缺少其中一个,该代码将无法工作。 但是,您似乎可以为这些变量分配不同的名称,并且它仍然可以工作。
这些变量如何相互连接/相互作用? 参考:
1.) 双倍幂(双底,int 指数)
2.) void print_pow (双底,int 指数)
3.) print_pow (base, exponent);
#include <iostream>
#include <cmath>
using namespace std;
double power(double base, int exponent)
{
double result = 1;
for(int i=0; i < exponent; i++)
{
result = result * base;
}
return result;
}
void print_pow(double base, int exponent)
{
double myPower = power (base, exponent);
cout << base << " raised to the " << exponent << " power is " << myPower << ". \n ";
}
int main()
{
double base;
int exponent;
cout << "What is the base?: ";
cin >> base;
cout << "What is the exponent?: ";
cin >> exponent;
print_pow(base, exponent);
}
单独想象一下这段代码:
double power ()
{
double result = 1;
for(int i=0; i < exponent; i++)
{
result = result * base;
}
return result;
}
你能告诉我什么是base
和exponent
,它们是从哪里来的吗?
答案是不。 如果你不能说,编译器也不能。 在我的代码中,未声明base
和exponent
。
这些被称为function 参数。 它们正是它们听起来的样子。 可以用数学符号做一个很好的类比:
f(x) = x * 2
括号中是 function 的参数。
现在考虑一个与您的代码非常相似的代码,但参数名称已更改:
double power(double base, int exponent)
{
double result = 1;
for(int i=0; i < exponent; i++)
{
result = result * base;
}
return result;
}
// Name changed! ----v------v
void print_pow(double b, int e)
{
double myPower = power(b, e);
cout << base << " raised to the " << exponent << " power is " << myPower << ". \n ";
}
如您所见,参数可以独立于它们的名称相互映射。 base
将采用b
的值, exponent
将采用e
的值。
function 参数的一个重要特性是它们就像局部变量一样。 这种本地实体不受外部实体名称的影响。 因此,如果在您的代码中它们是名为base
和exponent
的多个变量,它们是不同的实体,因为它们具有不同的范围。
如果你愿意,你可以写这样的 function:
void print_pow2(double base, int exponent)
{
double myPower = power(base * 2, 3);
cout << base << " raised to the " << exponent << " power is " << myPower << ". \n ";
}
如您所见,即使名称相同, base
和exponent
在power
中也不会具有相同的值。 您甚至可以注意到,当我发送常量3
时,内部power
的exponent
与print_power2
中的exponent
无关。
如果我再次对数学符号进行类比:
f(x) = x * 2
g(x) = f(x * 2) / 3
尽管g
和f
都有x
作为参数,但x
是不同的,并且在每个函数中占据不同的值。
这里应用的命名方案有点不幸。 这些变量的名称几乎可以是任何名称,建议花一些时间找到好的名称。 虽然,为了说明它们是不同的实体,我只是让它们不同:
#include<iostream>
#include<cmath>
using namespace std;
double power (double base, int exponent)
{
double result = 1;
for(int i=0; i < exponent; i++)
{
result = result * base;
}
return result;
}
void print_pow(double a, int b)
{
double myPower = power (a, b);
cout << a << " raised to the " << b << " power is " << myPower << ". \n ";
}
int main()
{
double x;
int y;
cout << "What is the base?: ";
cin >> x;
cout << "What is the exponent?: ";
cin >> y;
print_pow(x, y);
}
变量在某个 scope 中声明。 只有在这个 scope 中,您才能访问该变量。 在您的代码中有3
不同的变量,称为base
。 那时并没有什么神奇的关系,只是因为他们同名而已。 它们是通过使用参数print_pow(x,y)
调用 function 来“连接”的,参数名称与 function 和函数名称 ZDBC11CAA5BDA99F77E6FB4DABD8882 无关(除了给出与调用方无关的提示之外)该参数用于)。
这些变量如何相互连接/相互作用?
function 参数的名称与在 function 之外使用的其他变量的名称之间没有关系。
它们是不同的变量,它们的名称是否匹配并不重要。
当您定义参数时,例如double base, int exponent
。 在...
double power (double base, int exponent)
{
double result = 1;
for(int i=0; i < exponent; i++)
{
result = result * base;
}
return result;
}
...您告诉编译器,传递给您的 function power
的任何值都将成为 function 内部的新变量double base
和int exponent
。
当您的 function 完成时,这些新变量将不再存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.