【问题标题】:How to return a value with void function without parameter in c如何在c中使用没有参数的void函数返回一个值
【发布时间】:2016-09-06 12:44:59
【问题描述】:

我是 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;
}

【问题讨论】:

  • 我不明白,这看起来是个很糟糕的主意。仅仅因为你可以做某事并不意味着你必须去做。
  • 在 main() 之上声明原型。
  • 这里必须同意@Sourav,我们在过去 40 年的大部分时间里都在朝着 more 封装方向发展。全局变量通常是个坏主意。所以,是的,它可以完成,但是,为了好的代码(并且为了避免我或我的孩子可能不得不维持这样的怪物),我不会告诉你如何:-)
  • @paxdiablo “全局变量通常是个坏主意。”我不知道谁教你这些东西,但对我来说,你听起来像是一个在暴民中的人,并且和其他人被教过同样的东西。全局变量有时可能是唯一的解决方案。
  • @machine_1,因此我使用了“一般”这个词。它们有自己的位置,还有多个返回点、goto 等等,但绝大多数代码应该避免使用它们。无论如何,我很难想出他们是唯一解决方案的场景。也许是最好的解决方案,但不是“唯一”。

标签: c function return global-variables void


【解决方案1】:

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

#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?

【讨论】:

  • 我已经根据你的代码更改了我的代码,它可以工作!!也非常感谢你
【解决方案2】:

这应该是不言自明的:

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 覆盖,因此丢失。 您可以通过声明第二个返回变量 RR_d_2 并将 RealRoot_2 的结果存储在其中来规避这种情况。

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

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

【讨论】:

  • 我已经根据你的代码更改了我的代码,它可以工作!!非常感谢
猜你喜欢
  • 2017-07-13
  • 2021-04-02
  • 2016-08-28
  • 2013-12-03
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
相关资源
最近更新 更多