如何 return 带有 void 函数的值,在 c 中没有参数

How to return a value with void function without parameter in c

我是 C 语言和编码的新手,我遇到了一个问题,要求我更改函数头:

float RealRoot_1(float a, float b, float c);
float RealRoot_2(float a,float b,float c);

成为:

void RealRoot_1(void);
void RealRoot_2(void);

有人告诉我这与全局变量有关,但我尝试了一段时间后仍然无法弄清楚。谁能解释一下该怎么做?非常感谢。

源文件如下:

#include<stdio.h>
#include<math.h>

int main()
{
    float RealRoot_1(float a, float b, float c);   // Prototype declaration
    float RealRoot_2(float a, float b, float c);    

    // Defining Input Variables
    float x, y, z;

    // Defining Output Variables
    float Root_1, Root_2;

    printf("Please enter the factor of X^2: ");
    scanf("%f",&x);

    printf("Please enter the factor of X: ");
    scanf("%f",&y);

    printf("Please enter the free factor: ");
    scanf("%f",&z);

    Root_1 = RealRoot_1(x,y,z);
    Root_2 = RealRoot_2(x,y,z);

    printf("the First Root is: %f \n", Root_1);
    printf("the Second Root is: %f \n", Root_2);

    system("pause");
}


float RealRoot_1(float a, float b, float c)
{
    float x; 
    x = (-1*b + sqrt(pow(b,2) - 4 * a * c)) / (2 * a);

    return x;
}

float RealRoot_2(float a, float b, float c)
{
    float x; 
    x = (-1*b - sqrt(pow(b,2) - 4 * a * c)) / (2 * a);

    return x;
}

这可以通过使用全局变量来完成。需要保证函数中使用的变量名与主代码中使用的变量名相同

#include<stdio.h>
#include<math.h>

void RealRoot_1(void);   // Prototype declaration
void RealRoot_2(void);        

float x, y, z;  
float Root_1, Root_2;

int main()
{

    // Defining Output Variables

    printf("Please enter the factor of X^2: ");
    scanf("%f",&x);

    printf("Please enter the factor of X: ");
    scanf("%f",&y);

    printf("Please enter the free factor: ");
    scanf("%f",&z);

    RealRoot_1();
    RealRoot_2();

    printf("the First Root is: %f \n", Root_1);
    printf("the Second Root is: %f \n", Root_2);

    system("pause");
}


void RealRoot_1(void)
{
    Root_1 = (-1*y + sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}

void RealRoot_2(void)
{
    Root_2 = (-1*y - sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}

请注意这是比最初问题中给出的更糟糕的做事方式。在最初的练习中。您正在失去模块化,使用太多全局变量通常不是一个好主意。

您还可以看到Are global variables bad?

这应该是不言自明的:

float RR_a, RR_b, RR_c;
float RR_d; // store result here(like a return value)

void RealRoot_1(void); // prototypes
void RealRoot_2(void);

void main(void)
{
   printf("Please enter the factor of X^2: ");
   scanf("%f",&RR_a);

   printf("Please enter the factor of X: ");
   scanf("%f",&RR_b);

   printf("Please enter the free factor: ");
   scanf("%f",&RR_c);

   RealRoot_1();
   printf("the First Root is: %f \n", RR_d);

   RealRoot_2();
   printf("the Second Root is: %f \n", RR_d);

   system("pause");
}

void RealRoot_1(void)
{
    float x; 
    x = (-1*RR_b + sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);

    RR_d = x;
}

void RealRoot_2(void)
{
    float x; 
    x = (-1*RR_b - sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);

    RR_d = x;
}

请注意,在调用 RealRoot_1 之后,我们现在在调用 RealRoot_2 之前打印结果。那是因为RR_d中存储的RealRoot_1的结果被RealRoot_2覆盖了,所以丢失了。 您可以通过声明第二个 return 变量 RR_d_2 并将 RealRoot_2 的结果存储在其中来避免这种情况。

我们不需要 RR_a、RR_b 或 RR_c 的重复项,因为它们的值未在函数内修改。

这种写函数的方式有局限性,遇到递归或者多线程的时候会很明显。