关于无效功能的问题 [英] question about void functions

查看:96
本文介绍了关于无效功能的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我尝试通过代码在这里制作一个基地,然后要求一个指数。但我必须使用void函数。我试图通过引用传递,但我不完全确定我是否做得那么正确。所以,当我尝试错误说

 a11p1.c:3:31:错误:未知类型名称'  a' 
a11p1.c:16:30:错误:未知类型名称' < span class =code-comment> a'



我只是假设我收到此错误,因为我使用的是void函数但是我真的只是不知道。所以任何人都可以尝试帮助我修复这个程序,如果有人能向我解释甚至有无效功能的话。



 #include <   stdio.h  >  
#include < math.h >
void power(double x,double b,a);

int main()
{
int x,b,a;
printf(insert base:);
scanf(%d,& x);
printf(insert power:);
scanf(%d,& b);
power(x,b,& a);
printf(anser is%d \ n,a);
返回0;
}
空功率(双x,双b,a)
{
a = pow(x,b)
}

解决方案

除了解决方案2:



没有指针参数的Void函数完全没有意义,因为你将无法返回任何东西。仍然可以使用这样的void函数,但仅用于其副作用,例如 puts 。但在你的情况下,根本没有任何意义。让我们修复你的语法并尝试

  void  power( double  x, double  b, double  a) //   MAKES  
{ //
a = pow(x,b) // SENSE!
}



看,会发生什么:首先,调用将一些值传递给 a ,它是复制到堆栈然后忽略。而是将新值分配给 a ,稍后将其丢弃。所有数据和,值和返回地址都放在堆栈上,调用结束,其他函数被调用,堆栈内存被重用。所有的力量:-)无处可去。



请注意,你无法承受对堆栈如何工作的无知。调用和返回的所有这些机制,以及局部变量和所有函数参数,都基于堆栈机制,在所有这些CPU的体系结构中强制转换。你需要在进一步移动之前学习它。



现在,下一个变体,它将起作用:

  void  power( double  x, double  b, double  * a) //  丑陋 
{
* a = pow(x,b); // 可行,但为什么?
}
// 电话:
double a; // 无需初始化,
/ / 该值将被忽略
power( 2 3 ,& a); // a将为2³

它会起作用吗?是。它够好吗?没有!这才是真正有意义的:

  double  power( double  x, double  b)
{
return pow (X,b);
}

但是,当然,如果你不只是重命名 pow(double,double),那真的很有意义。这是此代码示例的唯一语义含义,但做了一些非常有用的事情。 :-)



-SA


你的问题不仅在于void声明器,而且主要是用指针...... :-)

让我们从你想要的东西开始:你似乎想要创建一个函数来计算x到指数b的幂并在变量中返回结果a。

事实上你正确地将指针传递给变量a:

 power(x,b,& a); 
printf( anser is%d \ n,a);



但你省略将其声明为指针甚至将其作为指针处理 -

正确的函数声明和编程是:

  void  power( double  x, double  b, double  * a) //   a是指向double的指针! 
{
* a = pow(x,b); // 必须将结果分配给指定值!
}





关于void函数存在的原因是允许函数只是没有任何东西可以返回...... :-



PS编译器抱怨的原因是解决方案1中已经报告的内容:在函数声明中,您无法声明相同类型的变量,并使用逗号分隔变量声明。

即:

  double  x,y,z;  //  这是允许的x,y和z都是双打 
void power( double x, double b,a); // 这是一个错误!


我怀疑错误在这里:



  void  power( double  x, double  b, a 
^


Hello everyone I tried making by code here to take ask for a base and then ask for an exponent. But I have to use a void function for it. I tried to use a pass by reference but I'm not entirely sure if I did that quite right. So when I try an error saying

a11p1.c:3:31: error: unknown type name 'a'
a11p1.c:16:30: error: unknown type name 'a'


I'm just assuming that I get this error because I'm using a void function but I really just don't know. So could anyone try and help me fix this program, and if someone could explain to me the point of even having a void function.

#include <stdio.h>
#include <math.h>
void power(double x, double b,a);

int main()
{
int x,b,a;
printf("insert base: ");
scanf("%d",&x);
printf("insert power: ");
scanf("%d",&b);
power(x,b,&a);
printf("anser is %d\n",a);
return 0;
}
void power(double x,double b,a)
{
a=pow(x,b)
}

解决方案

In addition to Solution 2:

Void function without a pointer parameters make no sense at all, because you would not be able to return anything. Such void function still can be used, but only for its side effect, such as with puts. But in your case, it makes no sense at all. Let's fix you syntax and try

void power(double x,double b, double a) // MAKES
{                                       // NO
    a = pow(x,b)                        // SENSE!
}


Look, what happens: first, the calls passes some value to a, it is copied on stack and then ignored. Instead, new value is assigned to a, which is later discarded. All data and, value and return address are put on stack, and the call ends, and other functions are called, and the stack memory is reused. All the power :-) goes nowhere.

Note that you cannot afford to keep ignorance on how stack works. All this mechanism of calls and returns, as well as local variables and all function parameters, all that is based on the stack mechanism, which is cast in the architecture of all those CPUs. You need to learn it before you move any further.

Now, next variant, which will work:

void power(double x, double b, double *a) //Ugly
{
    *a = pow(x,b); // Will work, but why?
}
// the call:
double a; // no need to initialize,
          // the value will be ignored
power (2, 3, &a); // a will be 2³

Will it work? Yes. Is it good enough? No! This is what can really make sense:

double power(double x, double b)
{
    return pow(x,b); 
}

But of course, it makes real sense if you not just rename pow(double, double); which is the only semantic meaning of this code sample, but do something really useful. :-)

—SA


Your problem is not only with void declarator, but mainly with pointers... :-)
Let start from what you seems to want: it seems that you want create a function that computes the power of x to exponent b and give back the result in the variable a.
Infact you correctly pass the pointer to the variable a:

power(x,b,&a);
printf("anser is %d\n",a);


But you omit to declare it as a pointer and even to handle it as a pointer-
The correct function declaration and programming is:

void power(double x,double b, double *a)    //a is a pointer to double!
{
    *a = pow(x,b);  // the result must be assigned to the pointed value!
}



About the reason for existence of void functions is to allow for functions that simply have nothing to return... :-D

P.S. The reason for which the compiler complains is what already reported in the solution 1: in functions declarations you cannot declare variables of same type dividing them with a comma as you make for variables declaration.
I.e.:

double x, y, z;    //This is allowed x,y and z are all doubles
void power(double x, double b,a);  //This is an error!


I suspect that the error is here:

void power(double x,double b,a)
                             ^


这篇关于关于无效功能的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆