繁体   English   中英

如何将变量从一个函数转换为另一个函数?

[英]How do I get a variable from one function into another?

就像标题所说的一样,如何将我的discr变量从discr函数转换为calcRoots函数?

// Function calculating roots
bool calcRoots(double coeffs[], int coeffLength, double roots[], int rootLength) {
   if(disc >= 0) {
      roots[rootLength - 1] = ((-(coeffs[coeffLength - 2]) + sqrt(disc)) / (2*(coeffs[coeffLength - coeffLength])));
      roots[rootLength - rootLength] = ((-(coeffs[coeffLength - 2]) - sqrt(disc)) / (2*(coeffs[coeffLength - coeffLength])));
      return true;
   }
   else {
      return false;
   }
}

// Solves the discriminant
double discr(double coeffs[], int coeffLength){
   double disc = (pow(coeffs[coeffLength - 2],2)-4*coeffs[coeffLength - coeffLength]*coeffs[coeffLength - 1]);
   return disc;
}

您没有提到语言,但我想是C ++。 在calcRoots内部,您可以调用discr并将值获取为这样的变量:

double disc = discr(coeffs[], coeffLength);

然后使用变量中的值存储。 希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM