如何从一个函数到另一个函数获取变量?

时间:2014-03-26 20:29:18

标签: c++ function

正如标题所说,我如何从我的discrimin函数中将我的光盘变量放入我的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;
}

1 个答案:

答案 0 :(得分:1)

你没有提到nay语言,但我认为是C ++。 在calcRoots中,您可以调用discrimin并将值赋给变量,如下所示:

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

然后在变量中使用值存储。希望这有帮助。