使用指针将函数返回到main?

时间:2021-03-24 19:27:10

标签: c

我有一个要在 main 中回调的函数,但我想使用一个指针将其返回到 main。

int main(void) {
  int i, time, n;
  double APR, IM, M, loanAmount, z;
  double p;

  printf("Enter in loan amount:\n");
  scanf("%lf", &loanAmount);
  printf("Enter APR: \n");
  scanf("%lf", &APR);
  printf("Enter loan time (years): \n");
  scanf("%d", &time);
  IM = (APR / 100) / 12;//calculates and stores interest paid monthly
  n = time * 12;
  M = CalculatePayment(M, loanAmount, APR, time, IM, n);
  p = loanInformation(loanAmount, APR, IM, time, n, M);
  printf("%lf", p); 
  z = Ammortization(IM, loanAmount, M, n);
  return 0;

这是我的主体。我试图在 main 中返回的函数是loanInformation。贷款信息看起来像:

double loanInformation(double loanAmount, double APR, double IM, int time, int n, double M){
  
  printf("\nThe amount of the loan (principal):%10.2lf\n\n", loanAmount);
  printf("Interest rate/year (percent):%11.2lf\n\n", APR);
  printf("Interest rate/month (decimal):%14lf\n\n", IM);
  printf("Number of years:%22d\n\n", time);
  printf("Number of months:%22d\n\n", n);
  printf("Monthly payment:%26.2lf\n\n", M);
}

是否可以使用指针将其返回到 main 中?或者我必须返回一些东西?

1 个答案:

答案 0 :(得分:-1)

在loanInformation 函数中你应该放置一个return x;语句,其中 x 需要是 double 类型。你什么也没返回,所以,你应该得到一个编译错误。 如果您的函数的目的只是打印,您应该将其定义为:

无效贷款信息(······){···}